Windows: Auto start PM2 and node apps

前端 未结 3 1138
礼貌的吻别
礼貌的吻别 2021-01-30 18:40

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 aft

3条回答
  •  余生分开走
    2021-01-30 19:04

    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):

    1. Add and set PM2_HOME in System environments (not user environments). Like: PM2_HOME = C:\NodeJS\npm
    2. Add 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).
    3. Close all terminals and open them again (as administrator). Your terminal windows will now be aware of your environment changes.

    Prerequisites (part # 3):

    1. npm install pm2 -g
    2. npm i pm2-windows-service -g
    3. 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:

    1. In terminal cd into: C:\NodeJS\npm\node_modules\pm2-windows-service
    2. 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.
    3. ncu inquirer -u this will update your packages.json file.
    4. 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:

    1. In terminal cd into: C:\NodeJS\npm\node_modules\pm2-windows-service
    2. 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.)
    3. Perform environment setup (recommended)? Yes
    4. Set PM2_HOME? No (No need - You have set it already)
    5. Set PM2_SERVICE_SCRIPTS (the list of start-up scripts for pm2)? Yes
    6. 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's dump.pm2 file - which is created when you run PM2 -f save, I will return and explain this later on).
    7. Set PM2_SERVICE_PM2_DIR (the location of the global pm2 to use with the service)? Yes

    8. Specify the directory containing the pm2 version to be used by the service? ENTER

    PM2 service installed and started.

    1. 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:

    1. pm2 start myApp.js --name mySuperApp
    2. pm2 -f save
    3. 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):

    1. npm uninstall pm2-windows-startup -g
    2. 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)

    1. npm install pm2 -g
    2. npm install pm2-windows-startup -g
    3. pm2-startup install
    4. pm2 start myApp.js --name mySuperApp
    5. pm2 save
    6. reboot
    7. pm2 ls

提交回复
热议问题