Register PM2 as a service in the windows server
In the windows server, PM2 tool is not registered as a service automatically. So when the server restarts due to any reason, it does not start without human intervention.
Please follow the below steps to register as a service that auto starts nodejs application on system restart.
Step 1 :
Download NSSM — the Non-Sucking Service Manager on your machine and extract it to C drive. Add 64-bit binary path to environment variable so that it can be accessed from command prompt directly by typing nssm.
Step 2 :
Install service by typing the following command.
nssm install {service name}
It will open a popup window and ask you to configure the application path, startup directory path, and arguments.
Type following command to know the full path of pm2 tool.
where pm2C:\Users\Administrator\AppData\Roaming\npm\pm2.cmd
Path points to above PM2 binary location and point startup directory to the working directory of the project. Then put start FileName command in the arguments section.
Step 3:
Navigate into “Exit Actions” section and select “No Action” if the application exits.
It is required as PM2 just exits cmd once it starts and it runs in the background. Then click on install service.
Step 4:
Open windows task manager and search for the service name that you installed. By default, its status will be in “Stopped” state. Right-click on the service name and start it. Then it will come to “Running” state.
Now you have successfully installed PM2 as a service on windows using NSSM tool. You can restart windows and the service will be restarted automatically.