pm2
pm2 copied to clipboard
Startup script generation for Windows
Feature request: Startup script generation for Windows platform. Maybe by making use of node-windows.
I had a hard time trying to use Windows Task Scheduler to start my app at system startup. So for anyone else trying to do this, here is how I did it:
Create a batch file like this:
set HOMEDRIVE=C: set HOMEPATH=\Users%USERNAME% cd C:\directorytoapp
pm2 start app.js
The key to it working is the first two lines. Not sure why but Windows did not have these available at the time of execution.
Interesting fact, if you disable and re-enable the task schedule, it will then have those variables already set.
:+1: on using node-windows
I created a module which does this. Because it is very Windows specific, I was not sure whether this could be added to the main PM2 package. This is why I created it as a separate package. I'm open to merging this with the main package.
How to start pm2 resurrect as Windows Service
Change Pm2 home folder
mkdir C:\etc\.pm2
setx /M PM2_HOME C:\etc\.pm2
Crate a batch file (pm2-startup.bat) to run on service started:
set HOMEDRIVE=C:
set PM2_HOME=C:\etc\.pm2
setx /M PM2_HOME C:\etc\.pm2
pm2 kill & pm2 resurrect
Use nssm to transform batch script on Windows services
http://www.nssm.cc/download
nssm.exe install
Path: C:\etc\.pm2\pm2-startup.bat
Startup directory: C:\etc\.pm2\
Service name: Pm2Startup
Exit action > Restart: none
Details > Startup type: Delayed startup
Here is another solution I wrote in C# to auto startup native node server or pm2 server on Windows
Just a heads up: Setting your pm2 env variables on batch filed may cause some problems if you are pointing to any drive other than the drive pm2 is installed in.
Have anyone tried https://www.npmjs.com/package/pm2-windows-startup?
I've successfully used Task Scheduler to resurrect on reboot. My task just set the action script to the full path to pm2.cmd and the argument to "resurrect" (I also set the "start in" folder).
There are two important points:
- Be sure all of the directories needed (your app's, .pm2, etc.) have Full rights for the login account configured for the task.
- If the login account is not the pm2 home account, define PM2_HOME in the system environment.
@cbernstein thank you for your tip to give full rights to the given login account for pm2. I decided, instead to just to give rights of modify, edit, and create anything in the pm2 folder found in my node_module folder in C:\Program Files\nodejs\node_modules\pm2. This fixed all these issues of running as a normal user without admin rights.
Have anyone tried https://www.npmjs.com/package/pm2-windows-startup?
@Unitech I have tried this but I do not like all the dependencies it brings with it. Seems overkill.
The following is my latest solution which works really well:
- create a specific Windows user for running node scripts
- login as that user and
npm install pm2 -g pm2 startall the apps I would like to have startuppm2 saveto save the current process list- create a .bat file and within it paste the following commands:
@echo off
set HOMEDRIVE=C:
set HOMEPATH=\Users\%USERNAME%
set path=C:\Users\%USERNAME%\AppData\Roaming\npm;%path%
pm2 delete all & pm2 resurrect
- Use Windows Task Scheduler to create a task that:
- runs "whether user is logged on or not"
- is Triggered "At startup"
- is configured to run the .bat file that we created
Closing in favor of #2559
To try the new startup system:
$ npm install Unitech/pm2#development -g
$ pm2 update
$ pm2 startup # Then copy / paste the command displayed
$ pm2 unstartup
@Unitech why is this closed when #2559 specifically lists the Windows Init System as being unavailable? Sounds unresolved to me, and I've been back here trying to find a work around for a couple days now. Unless you mean move all this useful discussion to that broad announcement issue?
Our aim was to release the new startup system before going to holidays, we looked at a way to make it without dependency but it needed some time to investigate the exact command to run, we decided to release it without windows support and forgot the reopen this issue. Anyway we will try to push it in January :)
Glad to hear we weren't forgotten! After wasting my time all week trying to get PM2, NPM and the Local Service user to mesh I can't fault you guys for letting it slip. Windows services are not friendly to an NPM scripted ecosystem at all.
Did you tried this module or this one ? I was myself running under Windows and stopped because its really hard to find compatible tools.
I went with NSSM (https://nssm.cc/ - Public Domain) and ran it under a user with specific permissions. I call NPM run-scripts which automatically bring up PM2 for the app in order to make them runnable without requiring global packages. This uses a local PM2 and then minimizes work to deploy the service on a new host. Toss npm start into a start.bat and it's ready to be installed with NSSM.
My process is saved at first time, but after pc restarted it's process is not available, any solutions?