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.
- Create a c:\pm2 directory (the directory can be called anything and
be placed anywhere)
- 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)
- Log out and in again to make the new variable take effect.
- From a command prompt, run “pm2 start path/to/app1” etc for all apps
that should be monitored.
- From that same command prompt, run “pm2 save”. You now have a
snapshot file in c:\pm2.
- 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.
- 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.
- Reboot. PM2 should then launch after the reboot, and also start the
apps that were monitored by PM2 when the “pm2 save” was issued.