node.js - Node child process can not run detached -
i'm trying fork node child process with
child_process.fork("child.js")
and have alive after parent exits. i've tried using detached option so:
child_process.fork("child.js", [], {detached:true});
which works when using spawn, when detached true using fork fails silently, not executing child.js.
i've tried
var p = child_process.fork("child.js") p.disconnect(); p.unref();
but child still dies when parent does. or insight appreciated.
edit:
node version: v5.3.0 platform: windows 8.1 code:
//parent var child_process = require("child_process"); var p; try{ console.log(1) p = child_process.fork("./child.js") console.log(2) } catch(e){ console.log(e) } p.on('error', console.log.bind(console)) p.disconnect(); p.unref(); //to keep process alive settimeout(() => { console.log(1); }, 100000);
--
//child var fs = require("fs"); console.log(3); fs.writefilesync("test.txt", new date().tostring()); settimeout(()=>{ console.log(1); }, 100000);
i'm assuming you're executing parent file command line, why "appears" forked child not executing. in reality when parent process exits, terminal stops waiting , prints new line, waiting next command. makes seem child isn't executing, trust me is. there no "detached" option child_process.fork
add console.log() statements child process , should see input printing in terminal after parent has exited. if don't it's because child prematurely exiting due error. run child process directly debug it, before calling parent.
check out quick example:
hope helps.
Comments
Post a Comment