Search code examples
node.jsexpressiispm2npm-scripts

IIS Node express project down after the user logs out of the server


I'm deploying my node express project with iis and start pm2 start command. The project down when the user logs out server. it ups when the user logs in server. how does it up when the user logs out of the server? what should I do?


Solution

  • I think this is an approach that runs counter to the logic of the windows system.

    When the user logs off, Windows will close all programs and the programs cannot run. To a large extent, user logout is the same as shutting down. Shutdown will make all programs inoperable and put the device into a stopped working state. User logout is to allow other users to log in. Windows stops previous programs to allow all resources to serve subsequent users who log in.

    For your question, the best solution I can think of is to start PM2 when windows starts. However, it is still unavoidable that the user logout service stops unless the user does not logout.

    1. Create a c:\pm2 directory (the directory can be called anything and be placed anywhere)
    2. Create a system wide environment variable called PM2_HOME, and assign it a value of “c:\pm2” (or whatever directory you created in the first step)
    3. Log out and in again to make the new variable take effect.
    4. From a command prompt, run “pm2 start path/to/app1” etc for all apps that should be monitored.
    5. From that same command prompt, run “pm2 save”. You now have a snapshot file in c:\pm2.
    6. Run “pm2 kill” to stop pm2 itself as well as monitored processes, then “pm2 resurrect” to make sure the desired apps are started by pm2.
    7. Create a task in Windows Scheduler, set it to trigger on system startup, and simply do a “pm2 resurrect”. Depending on your path settings, you might have to replace “pm2” with the full path to the pm2 executable. In addition to trigger on system startup, the task should also trigger (for example) every 5 minutes. This ensures that PM2 itself will be restarted if it dies. In other words: maximum down time will be 5 minutes.
    8. Reboot. PM2 should then launch after the reboot, and also start the apps that were monitored by PM2 when the “pm2 save” was issued.