在 node.js 代码崩溃的情况下重启 bitcoind 守护进程

Restart bitcoind daemon in case of crash with node.js code

我需要编写一个 node.js 代码来启动 bitcoind 守护进程命令并继续监视它,如果它崩溃,进程应该重新启动。

我知道有 foreverforver-monitorpm2 等命令行 npm 模块,但我需要知道如何在代码中使用它们而不是将它们安装在系统上全球。

原因是我将在 Electron App 中交付此代码,最终用户不会在他们的机器上安装任何 node.js 或 npm。

我使用了这段代码,它给出了错误:

代码:

var forever = require('forever-monitor');
  var child = forever.start(['./bitcoind'], {
    max : 1,
    silent : true
  });

  child.on('exit', function () {
    console.log('bitcoind has exited');
  });

  child.start();

错误:

CONSOLE$ ps aux | grep bitcoind
satinder         32579   0.0  0.0  2432804    808 s001  S+    5:54pm   0:00.00 grep bitcoind
CONSOLE$ node test.js 
/Users/satinder/example/node_modules/eventemitter2/lib/eventemitter2.js:290
          throw arguments[1]; // Unhandled 'error' event
          ^

Error: Cannot start process that is already running.
    at /Users/satinder/example/node_modules/forever-monitor/lib/forever-monitor/monitor.js:158:26
    at doNTCallback0 (node.js:428:9)
    at process._tickCallback (node.js:357:13)
    at Function.Module.runMain (module.js:459:11)
    at startup (node.js:136:18)
    at node.js:972:3
CONSOLE$ ps aux | grep bitcoind
satinder         31931   0.1  0.3  2516556  23964 s000  SN    4:58pm   0:01.25 ./bitcoind
satinder         31939   0.0  0.0  2450212    832 s000  S+    4:58pm   0:00.00 grep bitcoind
CONSOLE$ 

我认为原因是 bitcoind 启动时它没有将进程保持在前台,并将其推送到后台?从 forever 模块的监视器中,它显示进程已退出?我不确定。

有什么可以帮忙的吗?

提前致谢。

您似乎在进程终止之前调用了 child.start();

来自 forever-monitor 文档 how to spawning-a-non-node-process

你应该试试:

const forever = require('forever-monitor');
const child = forever.start(['./bitcoind'], {
  max : 1,
  silent : true
});

child.on('exit', function () {
  console.log('bitcoind has exited');
  child.start()
});