简体   繁体   中英

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.

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

pm2 writes application logs to ~/.pm2/logs and pm2 specific logs to pm2.log by default. We need to check both the locations to debug the issue.

One other way to debug application is by starting the application manually, ie., something like npm run start or node path/yo/your/bin.js

It should give you the missing piece of information to fix the problem and move on.

We also faced a similar problem where pm2 was restarting a process to start a node.js web application almost every second.

We found that MongoDB was not running, and then the web application would try to connect to the database on start up but would fail. This would prompt pm2 to restart the process over and over, causing a restart every second.

If this is your issue, try to start MongoDB with mongod or mongod --dbpath [your db path] ?

Applicable if you have packaged and started your app with NPM.

I simply had to change the "script" file in the ecosystem.configure.js ( or the json file if you are using ). app.js will not work, I had to replace it with ./bin/www and then it worked.

Be sure to look at the logs to see what is going wrong ( pm2 describe {process} will show you where they are saved). Also, see if you can run the express app without pm2 by stopping the pm2 process and running your app manually (ie npm run start ).

If you can run the app manually but it doesn't work with pm2, it might be that the app is not being run from the correct directory (you can modify this with the pm2 cwd argument).

Another common issue is that the correct environment variables are not set, so check your json or ecosystem file . You can also look at the environment that pm2 is running with pm2 prettylist .

I know this is kinda late and everything but for anyone scrolling over this, I found n actual solution, after hours of researching.

So I wanted to share this cheatsheet found: https://devhints.io/pm2

pm2 start app.js --no-autorestart

Just ran into this error too. I ran dmesg and that told me my process was getting killed by the Linux kernel, as it was using more memory than I had given the Docker container it was running inside.

Allocating more memory to the container fixed the problem in this case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM