简体   繁体   English

如何自动重启我通过“npm run start”运行的崩溃应用程序?

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

I have the following in my package.json:我的 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:我通过“npm run start”启动我的节点应用程序,它从我的 package.json 运行这一行就好了:

"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?不确定最好的方法来解决这个问题……我是永远使用 npm 原生的东西,还是使用 linux 端的东西来重新启动进程?

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).即,循环运行您的命令( budo browser.js:bundle.js -p 80 --live -- -t babelify ),直到它以状态代码 0 退出(成功)。

Your problem is typically solved with process managers.您的问题通常由流程管理器解决。 The most popular one for Node.js has always been PM2 Node.js 最流行的一直是PM2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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