pm2 icon indicating copy to clipboard operation
pm2 copied to clipboard

Auto startup pm2 on windows server 2012 (or other versions)

Open disharmonized opened this issue 11 years ago • 26 comments

How can i perform autostartup pm2 on windows server 2012 (ot other versions)? I tried to do that such way:

  1. Created .bat-file: C:\Users\user1\AppData\Roaming\npm\pm2.cmd delete all & C:\Users\user1\AppData\Roaming\npm\pm2.cmd start c:\someproject\backend\pm2-production.json
  2. Add it to autorun to local machine via regedit. And it worked but only when user is logged to the computer after reboot. So i need to pm2 started on computer startup whatever user is logged.

disharmonized avatar Apr 13 '15 07:04 disharmonized

Okay, i figured it out how to perform that :sunglasses: At first, be sure that PM2_HOME or HOME or HOME_ENV environment variables are set up as path to your profile, for example C:\Users\user1 (that was the cause in my situation). Then prepare your .bat file with pm2 start command (i am using pm2 start c:\myproject\pm2-production.json). And finally you can use any of ways to run your .bat file at system startup. I tried to do that via standard Task Scheduler in Windows Server supply by creating startup task (dont forget to specify 'run with admin priviligies') and by creating service via nssm. All of that ways was worked for me. After that you can use pm2 cli in win cmd as well (IMPORTANT! Run cmd as admin or you can get EPERM error). Btw, i am not sure but if you will use nssm, you may need to run cmd in 'shadow' mode via cmd shadow 0 to get access to the services session where pm2 is launched. I hope this information will be helpful to somebody.

disharmonized avatar Apr 15 '15 13:04 disharmonized

Hi. thanks your information. I'm trying to autostart pm2 when window server restart without logged in. But I failed it continuously.

So, Could you summarize how can i set autostart pm2 service with windows server again? I really want it.

Thanks.

YoungsoonLee avatar Jul 17 '15 18:07 YoungsoonLee

Hello @YoungsoonLee !

Did you get it? how do you resolve the problem?

Thanks!

marcoocram avatar Jul 21 '15 11:07 marcoocram

@marcoocram see https://github.com/Unitech/PM2/issues/1079

soyuka avatar Jul 21 '15 11:07 soyuka

Thanks @soyuka i've try this instructions, but when i log out, the service stops until i log in again....:S

I don't know what can i do...

marcoocram avatar Jul 21 '15 12:07 marcoocram

Have you tried https://github.com/marklagendijk/node-pm2-windows-startup too?

soyuka avatar Jul 21 '15 12:07 soyuka

Yes, with the same result, i think that is because the user that executes the process is not "SYSTEM" and when i log out his process is closed.

marcoocram avatar Jul 21 '15 12:07 marcoocram

I don't think that this is something we can resolve from PM2.

soyuka avatar Jul 21 '15 13:07 soyuka

me too. same result with @marcoocram , @soyuka

YoungsoonLee avatar Jul 22 '15 19:07 YoungsoonLee

I got the following process to work:

  1. Using ... cough cough .. forever I start by making a *.bat file
cd C:\project\root
call C:\Users\[COMPUTER_NAME]\AppData\Roaming\npm\forever.cmd start server.js
call C:\Users\[COMPUTER_NAME]\AppData\Roaming\npm\forever.cmd start server2.js
exit 0
  1. Setup a Windows Scheduled task that runs this bat file on windows startup at the highest privileges.

That's all it takes, BUT I want to use PM2 and NOT forever.

cd C:\project\root
call C:\Users\[COMPUTER_NAME]\AppData\Roaming\npm\pm2.cmd start server.js --name server
call C:\Users\[COMPUTER_NAME]\AppData\Roaming\npm\pm2.cmd start server2.js --name server2
exit 0

The tasks runs with no errors however the servers aren't running. :(

Any idea why this would work with forever but not with pm2?

dlbrown06 avatar Aug 11 '15 20:08 dlbrown06

@dlbrown06 I am running into the same issue as you. Calling the start command from a command prompt starts the server correctly, but using the same command from a .bat file does not start the server. Did you even solve this issue?

catdad avatar Sep 28 '15 15:09 catdad

Nope. We had to go back to forever. I tried reaching out to the pm2 contributors, but unfortunately it seems to be falling on def ears. Doesn't appear to be production ready despite their marketing efforts to claim different

dlbrown06 avatar Sep 29 '15 00:09 dlbrown06

@dlbrown06, I actually kept investigating today, and ended up doing something like this:

var path = require('path');

// regular JSON definition
var production = require('./production.json');

// resolves to the working directory of the process that launched it
var root = path.resolve('.');

process.env['HOMEDRIVE'] = 'C:';
process.env['PM2_HOME'] = path.resolve(root, '.pm2');

// use pm2 as an internal module
var pm2 = require('pm2');

pm2.connect(function(cErr) {
    if (cErr) {
        console.log(cErr);
        throw new Error('Error: ' + cErr.msg);        
    }

    pm2.start(production, function(err, apps) {
        if (err) {
            console.log(err);
            throw new Error('Error: ' + err.msg);
        } else {
            console.log("app count:", apps.length);
            apps.forEach(function(app) {
                console.log('name: %s | pid: %s', app.pm2_env.name, app.process.pid);
            });
        }

        process.exit(0);
    });
});

And then I ran node start.js in a batch file (it was a bit more complex, of course, but should work as an example). It seems that the problem I was running into was that PM2_HOME didn't exist (it's not enough to define it, it actually has to exist), and for some reason, that is required when you run from an automated way (batch file, Windows service, etc.), but you never get that error until you try to use it programmatically.

Anyway, I agree with you. This is something that the PM2 team needs to address, as you currently have to jump through some ridiculous hoops to get PM2 to work reasonably on Windows.

catdad avatar Sep 29 '15 01:09 catdad

@catdad i mentioned about PM2_HOME in second post here =)

disharmonized avatar Sep 29 '15 05:09 disharmonized

Right @disharmonized, but @catdad 's method might allow for auto-startup without logging into the server. I assume anyway, I haven't tested it out.

My main issue is that it requires a user to login, which I cannot enforce and I think @catdad may have figured it out. I'm going to try it out now.

dlbrown06 avatar Sep 29 '15 12:09 dlbrown06

I just tested this out and it still is launching command prompts when running the pm2 process. I would then assume that you still need a user logged in to use this. :(

dlbrown06 avatar Sep 29 '15 12:09 dlbrown06

@disharmonized Yes, you are right. You did mention it (which is why I started looking into it), but you did not mention that it has to exist (which is a bit silly, after all). The example always given, "C:\etc.pm2", does not actually work when set from a bat file, and the service still does not start (nor does it error). It does indeed start if you set it to a folder that does exist though. It also does not seem to work if you set it to any miscellaneous existing folder -- I found that it will fail to start if the folder is not ".pm2" (or something similar, judging by the buried documentation). These little details do need to be pointed out.

@dlbrown06, my full implementation is that I created a Windows Service, which in turn calls my script to start (and stop) pm2 as the service action. The service is set to run as LocalSystem (probably a bad idea... maybe?) so it will start without an account being logged in.

However, with all of the PM2_HOME details worked out, it allows for pm2 to start from a service or .bat or, presumably, the task scheduler, so you should be able to use your current approach to run the script with the same effect.

catdad avatar Sep 29 '15 12:09 catdad

@dlbrown06 Interesting point. The Windows Service is all silent and does not open any windows for me, but I didn't think to check any other options (well, I checked a bat file, but I was logged in and running it manually). I can take a look at it later today and figure out if there is something easier.

This guy is using a vbs script to run it silently, so maybe that's a way to go: https://github.com/marklagendijk/node-pm2-windows-startup/blob/master/invisible.vbs (Looks like this was also posted above... so really I am just adding more detail.)

catdad avatar Sep 29 '15 12:09 catdad

If I have to use VBS scripting to make something work, in my eyes that 'thing' is fundamentally broken. :P

Forever is working like a charm though...

dlbrown06 avatar Sep 30 '15 12:09 dlbrown06

Yeah, I would not say that you are wrong. But to be fair, this entire thread is about workarounds for things that are fundamentally broken.

Just to check, are you using cluster mode for your app, or fork mode? It bothers me that cluster mode opens a bunch of extra Node windows.

catdad avatar Sep 30 '15 19:09 catdad