Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is PM2 not launching my Node process?

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.

like image 956
208_man Avatar asked Jan 09 '19 17:01

208_man


1 Answers

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.

like image 72
user-12410035 Avatar answered Oct 03 '22 09:10

user-12410035