pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

Startup script generation for Windows

Open bchr02 opened this issue 10 years ago • 17 comments

Feature request: Startup script generation for Windows platform. Maybe by making use of node-windows.

bchr02 avatar Mar 09 '15 17:03 bchr02

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.

bchr02 avatar Mar 09 '15 22:03 bchr02

:+1: on using node-windows

pandres95 avatar Jun 12 '15 15:06 pandres95

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.

marklagendijk avatar Jul 13 '15 23:07 marklagendijk

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

ninjuki avatar Jul 20 '15 15:07 ninjuki

Here is another solution I wrote in C# to auto startup native node server or pm2 server on Windows

0xhmn avatar Dec 02 '15 17:12 0xhmn

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.

0xhmn avatar Dec 08 '15 17:12 0xhmn

Have anyone tried https://www.npmjs.com/package/pm2-windows-startup?

Unitech avatar Mar 10 '16 09:03 Unitech

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 avatar Jun 15 '16 17:06 cbernstein

@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.

danielmhair avatar Jul 20 '16 18:07 danielmhair

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 start all the apps I would like to have startup
  • pm2 save to 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

bchr02 avatar Nov 04 '16 21:11 bchr02

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 avatar Dec 06 '16 11:12 Unitech

@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?

celliott181 avatar Dec 30 '16 13:12 celliott181

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

vmarchaud avatar Dec 30 '16 14:12 vmarchaud

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.

celliott181 avatar Dec 30 '16 20:12 celliott181

Did you tried this module or this one ? I was myself running under Windows and stopped because its really hard to find compatible tools.

vmarchaud avatar Dec 30 '16 21:12 vmarchaud

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.

celliott181 avatar Dec 30 '16 21:12 celliott181

My process is saved at first time, but after pc restarted it's process is not available, any solutions?

maulanarcr7 avatar Jul 11 '22 09:07 maulanarcr7