Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js pm2 keeps restarting almost every second

I have deployed an express.js app on a Azure server. I use pm2 for process management.

The issue is pm2 keeps restarting almost every seconds.

staging@Server:/srv/apps/myapp/current$ pm2 list
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode │ pid   │ status │ restart │ uptime │ memory      │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ app      │ 0  │ fork │ 35428 │ online │ 0       │ 0s     │ 20.465 MB   │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
staging@Server:/srv/apps/myapp/current$ pm2 list
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode │ pid   │ status │ restart │ uptime │ memory      │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ app      │ 0  │ fork │ 35492 │ online │ 7       │ 0s     │ 59.832 MB   │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
staging@Server:/srv/apps/myapp/current$ pm2 list
┌──────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode │ pid   │ status │ restart │ uptime │ memory      │ watching │
├──────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ app      │ 0  │ fork │ 35557 │ online │ 13      │ 0s     │ 21.816 MB   │ disabled │
└──────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────────────┴──────────┘

~/.pm2/pm2.log

2016-05-10 17:39:34: Starting execution sequence in -fork mode- for app name:start id:0
2016-05-10 17:39:34: App name:start id:0 online
2016-05-10 17:39:35: App [start] with id [0] and pid [3149], exited with code [255] via signal [SIGINT]
2016-05-10 17:39:35: Starting execution sequence in -fork mode- for app name:start id:0
2016-05-10 17:39:35: App name:start id:0 online
2016-05-10 17:39:35: App [start] with id [0] and pid [3158], exited with code [255] via signal [SIGINT]
2016-05-10 17:39:35: Starting execution sequence in -fork mode- for app name:start id:0
2016-05-10 17:39:35: App name:start id:0 online
2016-05-10 17:39:36: App [start] with id [0] and pid [3175], exited with code [255] via signal [SIGINT]
2016-05-10 17:39:36: Starting execution sequence in -fork mode- for app name:start id:0

I am using coffee script in my application. And starting the app using pm2 start app.coffee

package.json

{
  "name": "myapp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "gulp start-server"
  },
  "dependencies": {
    "bcrypt-nodejs": "0.0.3",
    "body-parser": "~1.13.2",
    "co": "^4.6.0",
    "coffee-script": "^1.10.0",
    "connect-mongo": "^1.1.0",
    "cookie-parser": "~1.3.5",
    "debug": "~2.2.0",
    "express": "~4.13.1",
    "express-session": "^1.13.0",
    "gulp": "^3.9.1",
    "mongoose": "^4.4.14",
    "morgan": "~1.6.1",
    "newrelic": "^1.26.2",
    "passport": "^0.3.2",
    "passport-local": "^1.0.0",
    "pm2": "^1.1.3",
    "pug": "^2.0.0-alpha6",
    "request": "^2.72.0",
    "serve-favicon": "~2.3.0"
  },
  "devDependencies": {
    "shipit-cli": "^1.4.1",
    "shipit-deploy": "^2.1.3",
    "shipit-npm": "^0.2.0",
    "shipit-pm2-nginx": "^0.1.8"
  }
}

I am new to node.js. May be I am not seeing the obvious. Please help me out.

like image 656
Sebin Avatar asked May 10 '16 17:05

Sebin


People also ask

How do I stop PM2 from restarting?

Auto restart apps on file change If an application is started with the --watch option, stopping the app will not prevent it to be restarted on file change. To totally disable the watch feature, do: pm2 stop app --watch or toggle the watch option on application restart via pm2 restart app --watch .

Why is PM2 restarting?

PM2 allows to reload (auto fallback to restart if not in cluster) an application based on a memory limit/ Please note that the PM2 internal worker (which checks memory), starts every 30 seconds, so you may have to wait a bit before your process gets restarted automatically after reaching the memory threshold.

Does PM2 auto restart?

PM2 will keep your application forever alive, auto-restarting across crashes and machine restarts.


1 Answers

Check if your app modifies a file in the project folder (such as a log file). A change to any of the files triggers restart if watch flag is enabled.

To prevent this, use a process file and add watch_ignore flag in it.

Here's a documentation on how to use the process file: PM2 - Process File

like image 119
starleaf1 Avatar answered Oct 22 '22 22:10

starleaf1