linux - Daemonize 'child_process' that was 'fork'ed -
i trying achieve behavior similar 1 httpd has when starting nodejs. when say:
service httpd start in ps, we'll see:
[root@dev ~]# service httpd start starting httpd: [ ok ] [root@dev ~]# ps auxf | grep httpd root 3395 0.0 0.1 6336 304 pts/0 r+ 12:03 0:00 | \_ grep httpd root 3391 0.0 1.3 175216 3656 ? ss 12:02 0:00 /usr/sbin/httpd apache 3393 0.0 0.9 175216 2432 ? s 12:02 0:00 \_ /usr/sbin/httpd notice how httpd master , child have no terminal (? shown instead of pts/0 grep).
now... need ipc channel , therefore use child_process.fork no matter do, every time see terminal still attached daemon. here code welcome experiment on:
c.js - controller
var cp = require('child_process'); var d = cp.fork('d.js', {}); d.on('message', function() { d.disconnect(); d.unref(); }); d.js - daemon
process.send('ready'); settimeout(function() { console.log('test'); }, 10000); and see in terminal:
[root@dev ~]# node c.js # running control script [root@dev ~]# ps auxf | grep node # terminal interactive again check root 3472 0.0 0.3 103308 864 pts/0 s+ 12:13 0:00 | \_ grep node root 3466 1.1 5.6 648548 14904 pts/0 sl 12:13 0:00 /usr/bin/node d.js [root@dev ~]# test # appears outta because d.js still has stdout d.js still has pts/0 , writes test when it's interactive bash me.
how fix , make daemon drop terminal? don't care side (c.js or d.js) gets adjustments code, control both, need ipc channel , therefore has done via fork.
this can solved ditching fork in favor of spawn. same code can rewritten to:
var cp = require('child_process'); var d = cp.spawn(process.execpath, ['./d.js'], { detached: true, stdio: ['ignore', 'ignore', 'ignore', 'ipc'] }); d.unref(); d.disconnect(); the stdio part how fork works internally. have no idea why decided not expose full array , instead gave silent.
d.js have no terminal right @ start (it /dev/null - same httpd). not tested on windows.
Comments
Post a Comment