【问题标题】:Can I prevent forever from restarting my node script in certain cases?在某些情况下,我可以永远阻止重新启动我的节点脚本吗?
【发布时间】:2019-09-13 20:40:31
【问题描述】:

我有一个节点脚本,我从命令行永远运行:forever index.js

当该脚本崩溃时,我想永远重新启动它,但不是一直。我知道某些情况需要人工干预才能解决。在这些情况下,我希望可以以永远不会重新启动它的方式退出进程。

有什么办法吗?

我原本以为我可以用process.exit(1) 重新启动进程,而不是用process.exit(0) 重新启动,但显然情况并非如此。

这是另一种说法:

  • 在下面的代码中填空。
  • 将结果保存为index.js
  • 使用forever index.js 启动脚本
  • 脚本应该打印“hello”,退出,而不是重新启动

    setTimeout(function () {
        console.log("hello")
       // YOUR CODE GOES HERE
    }, 1500)
    

顺便说一句,那里的延迟只是在默认的--minUpTime 1秒左右

【问题讨论】:

    标签: node.js forever


    【解决方案1】:

    forever 支持使用 --killSignal 选项自定义退出信号:

    --killSignal     Support exit signal customization (default is SIGKILL),
                     used for restarting script gracefully e.g. --killSignal=SIGTERM
    

    以上是为了永远指示永远停止脚本​​的终止信号已经开始。要根据脚本退出的方式有选择地永久停止运行脚本,您需要使用forever-monitor

    首先,当您希望永远重新启动脚本时,您的脚本需要发送一个特定的信号。这是我们的 script.js:

    setTimeout(function () {
        console.log('hello');
    
        //process.kill(process.pid, 'SIGKILL'); // this will cause forever to restart the script.
    
        setTimeout(function () {
            process.kill(process.pid, 'SIGTERM');  // this will cause forever to stop the script.
        }, 1000);
    }, 2000);
    

    然后,我们需要一个带有永久监视器的附加脚本(我们称之为 script-monitor.js):

    var forever = require('forever-monitor');
    
    var child = new (forever.Monitor)('script.js', {
        max: 10,
        silent: false,
        args: []
    });
    
    child.on('restart', function() {
        console.error('Forever restarting script for ' + child.times + ' time');
    });
    
    child.on('exit:code', function(code) {
        console.error('Forever detected script exited with code ' + code);
        if (143 === code) child.stop(); // don't restart the script on SIGTERM
    });
    
    child.start();
    

    现在你可以运行 script.js 了:node script-monitor.js

    为方便起见,here is a list node.js 中的信号事件。

    【讨论】:

    • 我读过这个,但我不知道如何使用它。当我process.exit() 时,我可以传入一个整数。我不能传入 SIGTERM 或 SIGKILL。另外, killSignal 实际上是做什么的?如果我将其设置为 SIGTERM,什么默认行为将停止发生?出现了什么新行为?
    • 我想最好的解决方案取决于你什么时候不想永远重启这个过程。例如,如果您只想在脚本中故意process.exit() 时永远重新启动脚本,那么您可以指定--killSignal=SIGTERM 如果您需要更多自定义情况,那么您可能需要考虑查看永远监视器并编写一个带有永久监视器的节点脚本,在什么情况下实现重启/不重启,然后运行你的节点脚本。
    • 我已经编辑了我的问题以使其更清晰。我试过使用--killSignal=SIGTERMprocess.exit(),但脚本仍然会永远重启...
    • 嗨@Shawn。看来我有 --killSignal 倒退。您可以使用 --killSignal 来告诉永远将哪个信号传递给脚本,然后再永远停止它。我已经用您原来问题的解决方案修改了我的答案。
    【解决方案2】:

    根据@Marco 的回答,但不需要信号。它可以替换为您选择的退出代码(例如 0)。然后代码如下所示:

    script.js:

    function myStopCondition() {
      console.log('leaving');
      exit(0);
    }
    
    console.log('hello');
    setTimeout(myStopCondition, 2000);
    

    almost_forever.js:

    var forever = require('forever-monitor');
    
    var child = new (forever.Monitor)('script.js', {
      max: 10,
      silent: false,
      args: [] 
    });
    
    child.on('restart', function() {
      console.error('Forever restarting script for ' + child.times + ' time');
    });
    
    child.on('exit:code', function(code) {
      console.error('Forever detected script exited with code ' + code);
      if (0 === code) child.stop(); // don't restart the script on exit(1)
    });
    
    child.start();
    

    然后npm i forever-monitor [--save] 运行为

    node almost_forever.js
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多