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.

208_man
  • 1,440
  • 3
  • 28
  • 59
  • Did you check C:\\pm2_system\\.pm2\\logs\\sm-api-error? – pzaenger Jan 09 '19 at 17:36
  • @pzaenger - yes I looked. No errors logged. Thank you for the help. – 208_man Jan 09 '19 at 17:47
  • I saved this link a while back, https://github.com/jon-hall/pm2-windows-service ... I'm not sure if anything in there will help you. – Cody G Jan 09 '19 at 18:20
  • 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. – 208_man Jan 09 '19 at 18:50
  • Still no clarity yet on this issue. Input is deeply appreciated. – 208_man Jan 10 '19 at 14:34
  • Which pm2 version are you running? – Unitech Jan 10 '19 at 15:13
  • Hello @Unitech. v 3.2.7 Thanks! – 208_man Jan 10 '19 at 15:22
  • @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? – 208_man Jan 10 '19 at 15:55
  • 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. – Cody G Jan 10 '19 at 16:01
  • 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? – Cody G Jan 10 '19 at 16:05
  • @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. – 208_man Jan 10 '19 at 16:07
  • 2
    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. – adamrights Jan 10 '19 at 17:17
  • Thank you @adamrights. That was a **great** suggestion, but unfortunately when I tried it, same outcome. – 208_man Jan 10 '19 at 20:31
  • @adamrights 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. – 208_man Jan 10 '19 at 20:34
  • 1
    @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. – adamrights Jan 11 '19 at 05:02
  • @adamrights THANK YOU – 208_man Jan 11 '19 at 15:00
  • @adamrights 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: https://github.com/Unitech/pm2/issues/4113 Thank you for your input! – 208_man Jan 14 '19 at 16:44
  • @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: https://github.com/Unitech/pm2/issues/4113 Thank you for your input! – 208_man Jan 14 '19 at 16:45
  • @Unitech 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: https://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! – 208_man Jan 14 '19 at 16:45
  • 2
    @pzaenger 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: https://github.com/Unitech/pm2/issues/4113 Thank you for your input! – 208_man Jan 14 '19 at 16:45

4 Answers4

7

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.

user-12410035
  • 277
  • 4
  • 2
6

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

208_man
  • 1,440
  • 3
  • 28
  • 59
4

As a workaround I used the following:

pm2 start --no-daemon app.js

Sagar P. Ghagare
  • 542
  • 2
  • 12
  • 25
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
tuananh
  • 747
  • 8
  • 17