Running PM2 as a window service
{
"apps": [
{
"name": "web",
"script": "C:\\ws\\engineer\\titan-web\\app.js",
"args": [],
"cwd": "C:\\ws\\engineer\\titan-web",
"merge_logs": true,
"instances": 1,
"exec_mode": "cluster_mode",
"env": {
"NODE_ENV": "production"
}
},
{
"name": "com",
"script": "C:\\ws\\engineer\\titan-com\\index.js",
"args": [],
"cwd": "C:\\ws\\engineer\\titan-com",
"merge_logs": true,
"instances": 1,
"exec_mode": "cluster_mode",
"env": {
"NODE_ENV": "production"
}
}
]
}
Commands to remember:
pm2 start apps/app1.js
)pm2 reload ecosystem.json.js
)pm2 save
)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:
c:\etc\.pm2
PM2_Home
variable at the System level and set the value c:\etc\.pm2
echo %PM2_HOME%
ecosystem.json
fileYour pm2 save
command should help you create this or else use the file attached to this gist
pm2 reload ecosystem.config.js --env=production
can be used to load the configurationpm2 save
to save the configurationIf everything has been done correctly, you can test it out by:
pm2 kill
pm2 resurrect
set path=C:\Users\<username>\AppData\Roaming\npm;%path%
nssm.exe install MyPM2Service
Startup Type: Automatic delayed
nssm.exe remove MyPM2Service
Kudos! All done! You can restart the server and test it out.
@echo off
set HOMEDRIVE=C:
set PM2_HOME=c:\etc\.pm2
@REM Ensure that pm2 command is part of your PATH variable
@REM if you're not sure, add it here, as follow:
set path=C:\Users\<username>\AppData\Roaming\npm;%path%
@REM Optionally, you can add 'pm2 kill' just before
@REM resurrect (adding a sleep between 2 commands):
@REM pm2 kill
@REM timeout /t 5 /nobreak > NUL
@REM pm2 resurrect
@REM otherwise, you can simple call resurrect as follow:
pm2 resurrect
echo "Done"