At a Windows AWS server i have a NODE app and i'm using PM2 to launch the app
I have tried the NPMs: "pm2-windows-startup" and "pm2-windows-service"
But after i restart my AWS instance and run
PM2 ls
No node app shows up in the list...
I followed the instructions ...
The PM2 logs dont contain any thing...
I have not added any ENV variables explicit (when i tried PM2 could not start any more - so i created a fresh AWS windows instance and installed every thing from scratch again...)
PM2 is located the default place (i have not changed any paths)
C:\Users\Administrator\.pm2
My PM2 file contains:
2017-03-13 07:37:48: =============================================================================== 2017-03-13 07:37:48: --- New PM2 Daemon started ----------------------------------------------------
2017-03-13 07:37:48: Time : Mon Mar 13 2017 07:37:48 GMT+0000 (Coordinated Universal Time) 2017-03-13 07:37:48: PM2 version : 2.4.2 2017-03-13 07:37:48: Node.js version : 6.10.0 2017-03-13 07:37:48: Current arch : x64 2017-03-13 07:37:48: PM2 home : C:\Users\Administrator.pm2 2017-03-13 07:37:48: PM2 PID file : C:\Users\Administrator.pm2\pm2.pid 2017-03-13 07:37:48: RPC socket file : \.\pipe\rpc.sock 2017-03-13 07:37:48: BUS socket file : \.\pipe\pub.sock 2017-03-13 07:37:48: Application log path : C:\Users\Administrator.pm2\logs 2017-03-13 07:37:48: Process dump file : C:\Users\Administrator.pm2\dump.pm2 2017-03-13 07:37:48: Concurrent actions : 2 2017-03-13 07:37:48: SIGTERM timeout : 1600 2017-03-13 07:37:48: ===============================================================================
2017-03-13 07:37:48: Starting execution sequence in -fork mode- for app name:mySuperApp id:0 2017-03-13 07:37:48: App name:mySuperApp id:0 online 2017-03-13 07:40:45: ===============================================================================
2017-03-13 07:40:45: --- New PM2 Daemon started ---------------------------------------------------- 2017-03-13 07:40:45: Time : Mon Mar 13 2017 07:40:45 GMT+0000 (Coordinated Universal Time) 2017-03-13 07:40:45: PM2 version : 2.4.2 2017-03-13 07:40:45: Node.js version : 6.10.0 2017-03-13 07:40:45: Current arch : x64 2017-03-13 07:40:45: PM2 home : C:\Users\Administrator.pm2 2017-03-13 07:40:45: PM2 PID file : C:\Users\Administrator.pm2\pm2.pid 2017-03-13 07:40:45: RPC socket file : \.\pipe\rpc.sock 2017-03-13 07:40:45: BUS socket file : \.\pipe\pub.sock 2017-03-13 07:40:45: Application log path : C:\Users\Administrator.pm2\logs 2017-03-13 07:40:45: Process dump file : C:\Users\Administrator.pm2\dump.pm2 2017-03-13 07:40:45: Concurrent actions : 2 2017-03-13 07:40:45: SIGTERM timeout : 1600 2017-03-13 07:40:45: ===============================================================================
My PM2 DUMB file contains:
[ { "exec_mode": "fork_mode", "watch": false, "treekill": true, "autorestart": true, "automation": true, "pmx": true, "vizion": true, "name": "mySuperApp", "node_args": [], "pm_exec_path": "c:\mypath\mySuperApp\server.js", "env": { "windir": "C:\Windows", "USERPROFILE": "C:\Users\Administrator", "USERNAME": "Administrator", "USERDOMAIN_ROAMINGPROFILE": "EC2AMAZ-REBQJDK", "USERDOMAIN": "EC2AMAZ-REBQJDK", "TMP": "C:\Users\ADMINI~1\AppData\Local\Temp\2", "TEMP": "C:\Users\ADMINI~1\AppData\Local\Temp\2", "SystemRoot": "C:\Windows", "SystemDrive": "C:", "SESSIONNAME": "RDP-Tcp#1", "PUBLIC": "C:\Users\Public", "PSModulePath": "C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\AWS Tools\PowerShell\", "PROMPT": "$P$G", "ProgramW6432": "C:\Program Files", "ProgramFiles(x86)": "C:\Program Files (x86)", "ProgramFiles": "C:\Program Files", "ProgramData": "C:\ProgramData", "PROCESSOR_REVISION": "3f02", "PROCESSOR_LEVEL": "6", "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 63 Stepping 2, GenuineIntel", "PROCESSOR_ARCHITECTURE": "AMD64", "PM2_USAGE": "CLI", "PM2_INTERACTOR_PROCESSING": "true", "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JSE;.WSF;.WSH;.MSC", "Path": "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Amazon\cfn-bootstrap\;C:\Program Files\nodejs\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\AppData\Roaming\npm", "OS": "Windows_NT", "NUMBER_OF_PROCESSORS": "1", "LOGONSERVER": "\\EC2AMAZ-REBQJDK", "LOCALAPPDATA": "C:\Users\Administrator\AppData\Local", "HOMEPATH": "\Users\Administrator", "HOMEDRIVE": "C:", "ComSpec": "C:\Windows\system32\cmd.exe", "COMPUTERNAME": "EC2AMAZ-REBQJDK", "CommonProgramW6432": "C:\Program Files\Common Files", "CommonProgramFiles(x86)": "C:\Program Files (x86)\Common Files", "CommonProgramFiles": "C:\Program Files\Common Files", "CLIENTNAME": "THESILVERFOX", "APPDATA": "C:\Users\Administrator\AppData\Roaming", "ALLUSERSPROFILE": "C:\ProgramData", "PM2_HOME": "C:\Users\Administrator\.pm2", "mySuperApp": {} }, "pm_cwd": "c:\mypath\mySuperApp", "exec_interpreter": "node", "pm_out_log_path": "C:\Users\Administrator\.pm2\logs\mySuperApp-out-0.log", "pm_err_log_path": "C:\Users\Administrator\.pm2\logs\mySuperApp-error-0.log", "pm_pid_path": "C:\Users\Administrator\.pm2\pids\mySuperApp-0.pid", "km_link": false, "NODE_APP_INSTANCE": 0, "vizion_running": false, "windir": "C:\Windows", "USERPROFILE": "C:\Users\Administrator", "USERNAME": "Administrator", "USERDOMAIN_ROAMINGPROFILE": "EC2AMAZ-REBQJDK", "USERDOMAIN": "EC2AMAZ-REBQJDK", "TMP": "C:\Users\ADMINI~1\AppData\Local\Temp\2", "TEMP": "C:\Users\ADMINI~1\AppData\Local\Temp\2", "SystemRoot": "C:\Windows", "SystemDrive": "C:", "SESSIONNAME": "RDP-Tcp#1", "PUBLIC": "C:\Users\Public", "PSModulePath": "C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files (x86)\AWS Tools\PowerShell\", "PROMPT": "$P$G", "ProgramW6432": "C:\Program Files", "ProgramFiles(x86)": "C:\Program Files (x86)", "ProgramFiles": "C:\Program Files", "ProgramData": "C:\ProgramData", "PROCESSOR_REVISION": "3f02", "PROCESSOR_LEVEL": "6", "PROCESSOR_IDENTIFIER": "Intel64 Family 6 Model 63 Stepping 2, GenuineIntel", "PROCESSOR_ARCHITECTURE": "AMD64", "PM2_USAGE": "CLI", "PM2_INTERACTOR_PROCESSING": "true", "PATHEXT": ".COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JSE;.WSF;.WSH;.MSC", "Path": "C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Amazon\cfn-bootstrap\;C:\Program Files\nodejs\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;C:\Users\Administrator\AppData\Roaming\npm", "OS": "Windows_NT", "NUMBER_OF_PROCESSORS": "1", "LOGONSERVER": "\\EC2AMAZ-REBQJDK", "LOCALAPPDATA": "C:\Users\Administrator\AppData\Local", "HOMEPATH": "\Users\Administrator", "HOMEDRIVE": "C:", "ComSpec": "C:\Windows\system32\cmd.exe", "COMPUTERNAME": "EC2AMAZ-REBQJDK", "CommonProgramW6432": "C:\Program Files\Common Files", "CommonProgramFiles(x86)": "C:\Program Files (x86)\Common Files", "CommonProgramFiles": "C:\Program Files\Common Files", "CLIENTNAME": "THESILVERFOX", "APPDATA": "C:\Users\Administrator\AppData\Roaming", "ALLUSERSPROFILE": "C:\ProgramData", "PM2_HOME": "C:\Users\Administrator\.pm2", "status": "online", "pm_uptime": 1489390668484, "axm_actions": [], "axm_monitor": { "Loop delay": { "alert": {}, "agg_type": "avg", "value": "36.91ms" } }, "axm_options": { "default_actions": true, "transactions": false, "http": false, "http_latency": 200, "http_code": 500, "ignore_routes": [], "profiling": true, "errors": true, "alert_enabled": true, "custom_probes": true, "network": false, "ports": false, "ignoreFilter": { "method": [ "OPTIONS" ], "url": [] }, "excludedHooks": [], "module_conf": {}, "module_name": "mySuperApp", "module_version": "2.4.2", "pmx_version": "1.0.3", "error": true }, "axm_dynamic": {}, "created_at": 1489390668484, "restart_time": 0, "unstable_restarts": 0, "versioning": null, "node_version": "6.10.0" } ]
PM2 is designed to work with the default init system on a Linux system (which it can auto-detect) to generate the startup script and configure PM2 as a service that can be restarted at system boot. The startup sub-command tells PM2 to detect available init system, generate configuration and enable the startup system.
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 hit ENTER .)
Restart the application automatically The node command has to be re-executed in bash whenever there is a change in the application. To restart the application automatically, use the nodemon module. This local installation of nodemon can be run by calling it from within npm script such as npm start or using npx nodemon.
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):
PM2_HOME
in System environments
(not user environments). Like: PM2_HOME
= C:\NodeJS\npm
C:\NodeJS\npm
to the existing system PATH variable (Then you are sure it will work - there has been some issues reported that PM2_HOME
not always working).Prerequisites (part # 3):
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:
cd
into: C:\NodeJS\npm\node_modules\pm2-windows-service
ncu inquirer
this only outputs the existing and the newest available version of the inquirer
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 the inquirer
module (please be aware if you don't use specific version syntax in your packages.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:
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 hit ENTER
.)Yes
No
(No need - You have set it already)Yes
ENTER
(when nothing is entered - it will default to use PM2's dump.pm2
file - which is created when you run PM2 -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.
Setup the app(s) you like PM2 to startup - when shutdown or after a reboot:
pm2 start myApp.js --name mySuperApp
pm2 -f save
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):
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)
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:
c:\etc
cd c:\etc
npm i pm2
C:\etc\node_modules\.bin
to the System variable PATH
pm2
to initialize and create the .pm2
folderC:\Users\USER\.pm2
to C:\etc\.pm2
PM2_HOME
value: c:\etc\.pm2
pm2 start app.js --name=MY_API
.pm2 save
to create a dump of the current apps running.pm2 kill
and then pm2 resurrect
(app should be running, check with pm2 status
)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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With