Why is PM2 not launching my Node process?
Asked Answered
M

4

19

Previously I have had success implementing PM2, but not currently.

My node app does run just fine if I start it manually, but nothing I do gets it to run via PM2, even though PM2 appears to be starting it up. Here's what I mean:

If I run pm2 start server/index.js, the response in the terminal reads:

$ pm2 start server/index.js
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting D:\Program Files\nodeApps\service-management-api\server\index.js in fork_mode (1 instance)
[PM2] Done.* 

Then the terminal prints out a table with App info. It doesn't look pretty pasted here so I'll list it out:

App Name: index
id: 0
version: 1.0.0
mode: fork
pid: 8984
status: online
restart: 0
update 0s
cpu: 0% 
mem: 26.0 MB
user: ME
watching: disabled

It appears that the node process should be running. But if I immediately enter pm2 list it shows no processes running. If I enter pm2 stop index, it says:

$ pm2 stop index
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2][ERROR] Process index not found

Alternatively, if I try using ecosystem.config.js in the commands, I get similar results. Here are the commands tried:

pm2 reload ecosystem.config.js
pm2 start ecosystem.config.js

Example result of running those commmands:

$ pm2 start ecosystem.config.js
[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2][WARN] Applications sm_api not running, starting...
[PM2] App [sm_api] launched (2 instances)

And CLI prints table showing two instances with status "online" and watching "enabled". And yet, app isn't running (when tested from browser) and "pm2 show " returns:

[PM2] Spawning PM2 daemon with pm2_home=c:\pm2_system\.pm2
[PM2] PM2 Successfully daemonized
[PM2][WARN] <app name> doesn't exist

Any clues what's gone awry with my pm2?

Heres my ecosystem.config.js file:

module.exports = {
  apps : [{
    name: 'sm_api',
    script: 'server/index.js',
    "log_date_format"  : "YYYY-MM-DD HH:mm Z",

    // Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
    args: 'one two',
    instances: 'max',
    error_file : "C:\\pm2_system\\.pm2\\logs\\sm-api-error",
    out_file: "C:\\pm2_system\\.pm2\\logs\\sm-api-out",
    autorestart: true,
    watch: "../",
    max_restarts: 10,
    max_memory_restart: '1G',
    env: {
      NODE_ENV: 'development'
    },
    env_production: {
      NODE_ENV: 'production'
    },
    exec_mode: 'cluster'
  }],
};

Running in Windows Server 2012 environment. Note that I intend to add pm2-windows-service package after I get pm2 working.

Milissamilissent answered 9/1, 2019 at 17:31 Comment(20)
Did you check C:\\pm2_system\\.pm2\\logs\\sm-api-error?Ell
@Ell - yes I looked. No errors logged. Thank you for the help.Milissamilissent
I saved this link a while back, github.com/jon-hall/pm2-windows-service ... I'm not sure if anything in there will help you.Territorialism
Thanks @CodyG. I will be adding pm2-windows-service once I get pm2 installed successfully. Currently not yet installed. Good to know about the .net dependencies though.Milissamilissent
Still no clarity yet on this issue. Input is deeply appreciated.Milissamilissent
Which pm2 version are you running?Gouveia
Hello @Unitech. v 3.2.7 Thanks!Milissamilissent
@CodyG. have you had issues like this running pm2 in windows? I take it from the fact that you shared a link to the pm2-windows-service that you admininster pm2 in a windows server environment?Milissamilissent
HA! I tried setting it up a long time ago but to no avail on win7/win 2008r2. Are you running everything as "admin" ? I'm the one who favorited this question just in case I ever try again. I got it to work on windows 7, but on a sever as a service was a different story.Territorialism
You do have a c:\pm2_system\.pm2 folder, right? Since the folder is on the C:\ root I think you run into a lot of security issues so make sure it's full access for everyone on it maybe?Territorialism
@CodyG., Thanks for the input. Yes, running everything as admin. Yes, I have c:\pm2_system\.pm2 folder. This all works fine on our QA servers. Some setting on our Prod servers is preventing PM2 from working. Even the logs are unhelpful.Milissamilissent
Maybe the app is crashing quickly. Can you simplify your configuration a little. Turn off "cluster" mode. Turn off "max_memory_restart" , turn off watching. See if you can get just the basic server/index.js running with pm2. Perhaps you're going over the 1G limit due to file watching.Jerricajerrie
Thank you @adamrights. That was a great suggestion, but unfortunately when I tried it, same outcome.Milissamilissent
@Jerricajerrie while that didn't fix it, you did inspire me to watch the task manager while initiating pm2. To my surprise, the CPU spiked to 100% at the very moment when the process appears to crash. Note: because I can run my app manually (without PM2), I'm confident it's not an issue with my code. It's my config of PM2 or something about the OS's configuration.Milissamilissent
@mike_butak Will see if anyone at work using node.js with windows has any thoughts — I've used pm2 a lot, but never with Windows.Jerricajerrie
@Jerricajerrie THANK YOUMilissamilissent
@Jerricajerrie just FYI this problem has been solved. The recently-introduced PM2 v 3.2.5 introduces a bug which causes PM2 to crash in Windows. The solution was to revert to 3.2.4. Issue open on github: github.com/Unitech/pm2/issues/4113 Thank you for your input!Milissamilissent
@CodyG. just FYI this problem has been solved. The recently-introduced PM2 v 3.2.5 introduces a bug which causes PM2 to crash in Windows. The solution was to revert to 3.2.4. Issue open on github: github.com/Unitech/pm2/issues/4113 Thank you for your input!Milissamilissent
@Gouveia I learned the cause of this problem from another user, on github. The recently-introduced PM2 v 3.2.5 introduces a bug which causes PM2 to crash in Windows. The solution was to revert to 3.2.4. Issue open on github: github.com/Unitech/pm2/issues/4113. Does your team include Windows testing before version releases? If not, it'd be good to know so we can evaluate whether PM2 is the right process manager for our org. Thank you!Milissamilissent
@Ell just FYI this problem has been solved. The recently-introduced PM2 v 3.2.5 introduces a bug which causes PM2 to crash in Windows. The solution was to revert to 3.2.4. Issue open on github: github.com/Unitech/pm2/issues/4113 Thank you for your input!Milissamilissent
M
7

Ok, I got the answer after posting an issue to the pm2 github issues page.

Sharing it here in case anyone else finds themselves in this situation:

https://github.com/Unitech/pm2/issues/4113

(Basically pm2 3.2.5 introduced a bug that causes this issue in Windows. My dev install was 3.2.4. The issue was resolved by reverting to 3.2.4 in production. Simple process, see instructions at link above.)

Milissamilissent answered 14/1, 2019 at 16:42 Comment(0)
G
8

Just a note for those who are saying to run this in no-daemon mode, (pm2 start --no-daemon), you should use this mode if you're trying to diagnose the reason why when you run with the daemon, your script fails.

Almost invariably, this is because you're telling PM2 to watch a dist folder or similar while it's building, so PM2 keeps restarting until it hits its limit (because files are being regenerated at pretty rapid speed) and then outputs the "errored" status.

Running in no-daemon is absolutely not recommended in production. Ensure that PM2 is running as a daemon so it can itself restart, and so it can restart your processes as a process itself. If you kill your terminal sessions or they are automatically timed out on your host, you will quickly find your service dies when that happens.

So, in short.. do it the right way and figure out what the problem is, rather than being lazy and turning PM2 into a glorified wrapper for the node binary.

Goosander answered 15/7, 2020 at 15:4 Comment(1)
I was unaware there was a --no-daemon mode. It was exactly what I needed to see that the node version update minutes before broke fs.readFileSync. It was set to read a file in the same dir but it couldn't find it after the node upd. Used path.resolve and it works now. I couldn't find the problem because the app worked find when launched directly.Strepphon
M
7

Ok, I got the answer after posting an issue to the pm2 github issues page.

Sharing it here in case anyone else finds themselves in this situation:

https://github.com/Unitech/pm2/issues/4113

(Basically pm2 3.2.5 introduced a bug that causes this issue in Windows. My dev install was 3.2.4. The issue was resolved by reverting to 3.2.4 in production. Simple process, see instructions at link above.)

Milissamilissent answered 14/1, 2019 at 16:42 Comment(0)
T
4

As a workaround I used the following:

pm2 start --no-daemon app.js

Tracey answered 18/1, 2019 at 13:46 Comment(1)
This certainly is not a long-term solution. The second the shell process is terminated, the pm2 process will die.Goosander
P
0

In my case (pm2 v3.2.2):

  1. pm2 stop all //stop all
  2. rm ./pm2/*.pid //delete all
  3. pm2 start app.config.js
Prophylactic answered 23/12, 2022 at 5:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.