简体   繁体   中英

how do i auto restart a crashed app that I run via "npm run start"?

I have the following in my package.json:

  "scripts": {
    "test": "node test.js",
    "ffmpeg": "ffmpeg -framerate 30 -i frames/test_%04d.png -y -s:v 1280x720 -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p output.mp4",
    "deploy": "surge -p .",
    "start": "budo browser.js:bundle.js -p 80 --live -- -t babelify",
    "build": "browserify browser.js -t babelify | uglifyjs -m -c warnings=false > bundle.js"
  },

I start my node app via "npm run start", and it runs this line from my package.json just fine:

"start": "budo browser.js:bundle.js -p 80 --live -- -t babelify",

However, i want a process to detect if the app crashes, and reruns the same command to restart it. Not sure the best way to go about this...do i use something native to npm like forever, or use something on the linux side to restart the process?

What is the best way to handle this?

The simplest solution is a bit ugly, but seems to work:

"start": "RC=1; while [ $RC -ne 0 ]; do budo browser.js:bundle.js -p 80 --live -- -t babelify; RC=$?; done",

Namely, run your command ( budo browser.js:bundle.js -p 80 --live -- -t babelify ) in a loop, until it exits with status code 0 (success).

Your problem is typically solved with process managers. The most popular one for Node.js has always been PM2

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