崩溃后如何自动重新启动节点脚本 - 或启动该节点的 init.d 服务? [英] How to auto restart Node script - or init.d service starting that node - after crash?

查看:52
本文介绍了崩溃后如何自动重新启动节点脚本 - 或启动该节点的 init.d 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在运行 Raspbian 的 Raspberry PI 上运行 node.js 脚本.该 node.js 脚本在启动时启动,所以我只需要插入电路板即可.

I'm currently having a node.js script running on my Raspberry PI running Raspbian. That node.js script gets started at boot so I just have to plug in the board.

但是,如果脚本崩溃导致节点退出,我如何让引导服务(在 bood 启动节点脚本)在节点服务崩溃时自动重启?

But, if the script would crash resulting in the exit of node, how could I make the boot service (which starts the node script at bood) auto restart itself when the node service crashed?

这是我在 /etc/init.d 中的引导服务示例:

Here's an example of my bootservice in /etc/init.d:

#! /bin/sh
### BEGIN INIT INFO
# Provides:          MyScriptService
# Required-Start:    $all
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts my node.js script.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

case "$1" in
  start)
    log_begin_msg "Starting my script"

    node /home/pi/myscript.js

    log_end_msg $?
    exit 0
    ;;
  stop)
    log_begin_msg "Stopping my script"


    log_end_msg $?
    exit 0
    ;;
  *)
    exit 1
    ;;
esac

更新:能够在 Node 内部自行修复它.https://stackoverflow.com/a/50413860/3037607

UPDATE: Been able to fix it myself within Node itself. https://stackoverflow.com/a/50413860/3037607

推荐答案

在对 Node 可能性进行更深入的搜索之后,我找到了一种方法来避免脚本崩溃,如果没有捕获到异常,只需重新启动一切:

After searching a bit deeper in the Node possibilities I found a way to just not make the script crash, and if an exception is not caught, just restart everything:

process.on('uncaughtException', function(err){
  console.log(err);
  restartMyScript();  
});

这篇关于崩溃后如何自动重新启动节点脚本 - 或启动该节点的 init.d 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆