pm2 start app.js is exiting after 15 restarts
Asked Answered
W

1

12

npm start will start my app just fine but when I do:

pm2 start app.js

I get:

[PM2] Spawning PM2 daemon
[PM2] PM2 Successfully daemonized
[PM2] Process app.js launched
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode │ pid  │ status │ restart │ uptime │ memory      │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ app      │ 0  │ fork │ 4681 │ online │ 0       │ 0s     │ 11.508 MB   │ disabled │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

in the logs I get:

[PM2] Starting streaming logs for [all] process
PM2: 2015-06-04 17:12:37: App name:app id:0 exited with code 0
PM2: 2015-06-04 17:12:37: Starting execution sequence in -fork mode- for app name:app id:0
PM2: 2015-06-04 17:12:37: App name:app id:0 online
PM2: 2015-06-04 17:12:37: App name:app id:0 exited with code 0
PM2: 2015-06-04 17:12:37: Starting execution sequence in -fork mode- for app name:app id:0
PM2: 2015-06-04 17:12:37: App name:app id:0 online
PM2: 2015-06-04 17:12:37: App name:app id:0 exited with code 0
PM2: 2015-06-04 17:12:37: Starting execution sequence in -fork mode- for app name:app id:0
PM2: 2015-06-04 17:12:37: App name:app id:0 online
PM2: 2015-06-04 17:12:38: App name:app id:0 exited with code 0
PM2: 2015-06-04 17:12:38: Starting execution sequence in -fork mode- for app name:app id:0
PM2: 2015-06-04 17:12:38: App name:app id:0 online
PM2: 2015-06-04 17:12:38: App name:app id:0 exited with code 0
PM2: 2015-06-04 17:12:38: Starting execution sequence in -fork mode- for app name:app id:0
PM2: 2015-06-04 17:12:38: App name:app id:0 online
PM2: 2015-06-04 17:12:39: App name:app id:0 exited with code 0
PM2: 2015-06-04 17:12:39: Starting execution sequence in -fork mode- for app name:app id:0
PM2: 2015-06-04 17:12:39: App name:app id:0 online
PM2: 2015-06-04 17:12:39: App name:app id:0 exited with code 0
PM2: 2015-06-04 17:12:39: Script /home/user/app/app.js had too many unstable restarts (15). Stopped. "errored"

here is my package.json:

  1 {
  2   "name": "app",
  3   "version": "0.0.0",
  4   "private": true,
  5   "scripts": {
  6     "start": "node ./bin/www"
  7   },
  8   "dependencies": {
  9     "body-parser": "~1.12.4",
 10     "cookie-parser": "~1.3.5",
 11     "debug": "~2.2.0",
 12     "express": "~4.12.4",
 13     "jade": "~1.9.2",
 14     "morgan": "~1.5.3",
 15     "serve-favicon": "~2.2.1",
 16     "stylus": "0.42.3"
 17   }
 18 }

node version: v0.10.38 pm2 version: 0.12.15

How do I even debug this? I am not sure why it's failing, is there some other place I need to check? My app.js file?

Weighting answered 4/6, 2015 at 17:26 Comment(0)
V
12

First, I'd try node ./bin/www and just make sure that works correctly.

I'm not sure what the "official" way to figure this out is but this should work:
You could put an uncaught exception handler into your code which simply writes to a file or do something else.

process.on('uncaughtException', function(err) {
    console.log('Caught exception: ' + err);
    throw err;
});

Edit: Based on your comment the reason you're having issues is because app.js is not the real entry point in your application. The real entry point is ./bin/www

So you need to tell PM2 to start that file rather than app.js like this:
pm2 start ./bin/www

Vacancy answered 4/6, 2015 at 18:40 Comment(3)
so node app.js doesn't do anything -- refer to #22216092Weighting
I just updated my answer. npm start and node app.js start two different files. That's why node app.js appears to do nothing. Instead you could do node ./bin/www and it should start the same way that npm start does.Vacancy
I do not think that this is the answer. Out of the box pm2 start ./bin/www will also restart 15 times and die.Hickie

© 2022 - 2024 — McMap. All rights reserved.