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

695 views Asked by At

I need to make a node.js code which starts bitcoind daemon command and keep monitoring it, and in case if it crashes the process should restart.

I know there are command line npm modules like forever, forver-monitor, pm2 but I need to know how I can utilise them in the code and not install them on system globally.

The reason is I'm going to deliver this code in Electron App and the end user won't have any node.js or npm installed on their machine.

I used this code and it gives which gives me error:

Code:

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();

Error:

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$ 

I think the reason is bitcoind when starts it doesn't keep the process in foreground, and push it to background ? and from forever module's monitor it shows the process exited ? I'm not sure.

Can any please help?

Thanks in advance.

1

There are 1 answers

0
Fi3 On BEST ANSWER

It seems that you call child.start(); before that the process terminate.

From the forever-monitor documentation how to spawning-a-non-node-process.

You should try with:

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()
});