Windows: Auto start PM2 and node apps
UPDATE 16 FEB 2020:
If it is important to you that PM2 automatically starts up without you logging into the machine (after reboot) please follow my new set of instructions instead of the old ones.
New instructions (recommended):
Prerequisites (part # 1):
First, I have installed NPM
in a location which is available to all users. Depending on your use-case(s) it might not be necessary. But if you like to change your default location of NPM
- you should do it first (before continuing). Here is how you change it to the location (in terminal as administrator): C:\NodeJS\npm
:
npm config set prefix "C:\\NodeJS\\npm"
npm config set cache "C:\\NodeJS\\npm-cache"
npm config set temp "C:\\NodeJS\\temp"
npm config ls -l (this will list all NPM settings -> look for the 3 lines/changes marked as `overriden`)
Prerequisites (part # 2):
- Add and set
PM2_HOME
inSystem environments
(not user environments). Like:PM2_HOME
=C:\NodeJS\npm
- Add
C:\NodeJS\npm
to the existing system PATH variable (Then you are sure it will work - there has been some issues reported thatPM2_HOME
not always working). - Close all terminals and open them again (as administrator). Your terminal windows will now be aware of your environment changes.
Prerequisites (part # 3):
- npm install pm2 -g
- npm i pm2-windows-service -g
- npm install -g npm-check-updates
Currently there is a bug in a module which the package pm2-windows-service
uses - so lets fix this as well, please follow the steps below:
- In terminal
cd
into:C:\NodeJS\npm\node_modules\pm2-windows-service
ncu inquirer
this only outputs the existing and the newest available version of theinquirer
module we need to update, currently: version:1.1.2
-->7.0.4
.ncu inquirer -u
this will update your packages.json file.npm install
this will download and update theinquirer
module (please be aware if you don't use specific version syntax in yourpackages.json
file or you have made manually changes --> other modules would be updated as well.
Install and setup PM2 (as a service) to automatically startup after reboot:
- In terminal
cd
into:C:\NodeJS\npm\node_modules\pm2-windows-service
pm2-service-install -n PM2_STARTUP_SCRIPT
(PM2_STARTUP_SCRIPT
will be the "Display name" of the Windows service. Change it to what you prefer and hitENTER
.)- Perform environment setup (recommended)?
Yes
- Set PM2_HOME?
No
(No need - You have set it already) - Set PM2_SERVICE_SCRIPTS (the list of start-up scripts for pm2)?
Yes
- Set the list of startup scripts/files (semi-colon separated json config
files or js files)
ENTER
(when nothing is entered - it will default to use PM2'sdump.pm2
file - which is created when you runPM2 -f save
, I will return and explain this later on). Set PM2_SERVICE_PM2_DIR (the location of the global pm2 to use with the service)?
Yes
Specify the directory containing the pm2 version to be used by the service?
ENTER
PM2 service installed and started.
- Open Services in Windows and change the service to run as Administrator (or your preferred role).
Setup the app(s) you like PM2 to startup - when shutdown or after a reboot:
pm2 start myApp.js --name mySuperApp
pm2 -f save
- reboot or if you use AWS (or any other cloud provider). Reboot your instance - wait 5 minutes and then log into the machine and execute
pm2 ls
and check your app has been up and running for ~ 5 min (and not only few secs because you just logged in).
Uninstall and cleanup "pm2-windows-startup" from your registry (if you switch from my "old instruction" to the new ones):
- npm uninstall pm2-windows-startup -g
- Delete the
PM2
key from registry like in the picture below:
Old instructions (not recommended):
My old answer below is still working - but PM2 doesn't startup unless you log into the machine because it is loading PM2 from registry and doesn't run it as a service.
I don't know why - but after several attempts this worked out (at a fresh installed AWS Windows 2016 BASE instance)
- npm install pm2 -g
- npm install pm2-windows-startup -g
- pm2-startup install
- pm2 start myApp.js --name mySuperApp
- pm2 save
- reboot
- pm2 ls
pm2-windows-startup
works great if you're okay with the fact that it is launched on login. If you have a reboot on a server though (say Windows Update) you are out of luck.
pm2-windows-service
did work for me, using @innomizetech
fork, but I've had some issues, probably due to the user or its setup, or something else. Basically the service would start an old version of the saved process list, even though I tried pm2 delete all
, pm2 start ecosystem.config.js
, pm2 save
.
I resorted to a very simple home-made solution:
- Create a file
pm2-resurrect.sh
inC:\
, which contains the single linepm2 resurrect
. - In Task Scheduler, add a new task that is run on boot, and select that it is run whether the user is logged in or not. You need to type the user password at this point which is saved for successive runs.
- Set the task command to execute the file
C:\pm2-resurrect.sh
.
In my case I have Git for Windows which comes with bash, which opens the file and executes it. I didn't test it but I guess you could have a .cmd
file with the same content.
UPDATE 08/06/2022
I found out that npm i -g pm2
is global install per user.
it means that if another user logged in into the server, he won't have pm2 install - because -g
is not fully global as we want it to be.
so the solution for those who wants it fully globall:
Configure pm2
- create folder
c:\etc
- install pm2 in this folder
cd c:\etc
npm i pm2
- add
C:\etc\node_modules\.bin
to the System variablePATH
- type
pm2
to initialize and create the.pm2
folder - copy it from
C:\Users\USER\.pm2
toC:\etc\.pm2
- set a new System Variable (not user level) name:
PM2_HOME
value:c:\etc\.pm2
Configure pm2
Runnning your app with pm2
- run your pm2 app. ie:
pm2 start app.js --name=MY_API
. pm2 save
to create a dump of the current apps running.
Testing app
- to test everything is working, try:
pm2 kill
and thenpm2 resurrect
(app should be running, check withpm2 status
)
Run at startup
now we need to perform the resurrect command at startup, so:
npm install -g @innomizetech/pm2-windows-service
pm2-service-install -n PM2 --unattended
thats it.