Commands to remember:
- start/stop/list components (eg:
pm2 start apps/app1.js) - load a predefined configuration (
pm2 reload ecosystem.json.js) - save current configuration (
pm2 save) - restore previously saved configuration (
pm2 resurrect)
When you install PM2 (npm i -g pm2) it creates a default PM2 home folder under C:\Users\<username>\.pm2 that will store the relevant files.
We will need to move that folder:
- Create a new folder
c:\etc\.pm2 - Create a new
PM2_Homevariable at the System level and set the valuec:\etc\.pm2 - Restart
- Ensure the new home directory is recognized
echo %PM2_HOME%
Your pm2 save command should help you create this or else use the file attached to this gist
pm2 reload ecosystem.config.js --env=productioncan be used to load the configuration- Check that your application is working as expected
- if everything is alright, you can use
pm2 saveto save the configuration
If everything has been done correctly, you can test it out by:
- Kill PM2 process
pm2 kill - Restart PM2 process
pm2 resurrect
- Create a start up .bat file (attached to this gist). Make sure you set the correct path in the line
set path=C:\Users\<username>\AppData\Roaming\npm;%path% - Install NSSM (beyond scope of this gist)
- Add NSSM variable to system variable (beyond scope of this gist)
- As Admin User, install the service
nssm.exe install MyPM2Service - Set the Path to the bat file you had created. This should create the window service.
- Set the
Startup Type: Automatic delayed - Note: If you want to delete the service anytime you can run
nssm.exe remove MyPM2Service
Kudos! All done! You can restart the server and test it out.