Start PM2 on Windows Boot
Keep a Node.js process running after a restart.
Making pm2 auto-start a Node.js project on Windows boot
PM2 is a production process manager for Node.js applications with a built-in load balancer. It can be used to simplify many of the tedious tasks of managing Node applications, such as performance monitoring, automatic restarts, load balancing, and more.
Installation and setup
1. We usually install pm2 globally:
npm install pm2 -g
2. Install the Windows startup package:
npm install pm2-windows-startup -g
3. Create the boot startup script file:
pm2-startup install
Note: Step 3 alone is not enough — steps 4 and 5 must also be completed before boot auto-start takes effect.
4. Start the project with pm2 (to enable auto-start):
pm2 start <path> --name <name>
For example: pm2 start ./app.js –name xsx
5. Save the projects in pm2:
pm2 save
Note: pm2 save is very important. It saves the NodeJS services that pm2 is currently managing so they can be restored after a reboot.
After rebooting the machine you can check (displayed as a table):
pm2 ls
To uninstall the service, run:
pm2-service-uninstall
Common commands
Every pm2 service has a numeric id, which you can use to operate on it quickly. The examples below use service id 0 (of course, you can swap the id for the app name with the same effect):
pm2 start # Start a service; add --name to give the app a name, and --watch to watch for changes and restart the service
pm2 list # List the current services
pm2 monit # Monitor CPU and memory usage of each node process
pm2 stop 0 # Stop a service (pm2 stop <name or id>)
pm2 stop all # Stop all service processes
pm2 restart 0 # Restart a service (pm2 restart app.js)
pm2 restart all # Restart all processes
pm2 delete 0 # Delete a service (pm2 delete app_name|app_id)
pm2 delete all # Delete all services
pm2 logs # View the output logs of all services
pm2 logs 0 # View the output logs of a service
For more detailed usage see: https://github.com/Unitech/pm2
Further reading
Running pm2 as a Windows service
With the pm2-windows-startup approach above [recommended], you won't find any pm2-related service in the Windows Services list — it uses start-on-windows-boot to register PM2 as an auto-start program in the registry, which is how it achieves auto-startup.
pm2-windows-service, on the other hand, registers PM2 as a Windows service based on node-windows.
Testing showed that of the two options, only pm2-windows-startup works on Windows Server 2012, so pm2-windows-startup was chosen as the solution for auto-starting Node.js applications at boot.
So, for completeness, here are the steps for running pm2 as a Windows service:
1. npm i pm2 -g
2. npm i pm2-windows-service -g
3. Install the service: pm2-service-install -n myservice (after installation a new service named myservice appears in Windows Services)
Uninstall command: pm2-service-uninstall
4. Set the environment variable: PM2_SERVICE_SCRIPTS = the PM2 configuration file
5. Restart the myservice service in Windows Services