child_process dies when parent process die

1,280 views
Skip to first unread message

Rafael Brizola

unread,
Mar 1, 2012, 10:32:31 AM3/1/12
to nodejs
Hi guys,

This is my server.js:

[code]
var spawn = require("child_process").spawn;

var myapp = spawn('java', ['-jar', 'myapplication.jar']);
myapp.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});

myapp.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});

myapp.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
[/code]

P.S.:just put here the relevant lines.

My problem is: if I kill the nodejs process, my application (child
process) dies too. I figure out that this is the normal behaviour, but
there is a way to keep my child process running independently if the
parent process is running or not?

Thank you very much!!

Ben Noordhuis

unread,
Mar 1, 2012, 10:47:06 AM3/1/12
to nod...@googlegroups.com
On Thu, Mar 1, 2012 at 16:32, Rafael Brizola <rafael...@gmail.com> wrote:
> My problem is: if I kill the nodejs process, my application (child
> process) dies too. I figure out that this is the normal behaviour, but
> there is a way to keep my child process running independently if the
> parent process is running or not?

It depends on how you kill the parent process. Pressing Ctrl-C, for
instance, will send the SIGINT to both the parent and the child. What
you'd do in that particular case is register a process.on('SIGINT')
listener that calls process.exit().

Jimb Esser

unread,
Mar 2, 2012, 3:28:04 PM3/2/12
to nodejs
The child_process module launches the children with piped stdout and
stdin, and most processes will exit when they try to write to or read
from a broken pipe (is your spawned app printing something to
stdout?). I don't know of any existing module to launch children
unpiped, but it might not be too hard to make one.

Alternatively, have node launch a process that then launches your
child process detached (On Linux, something like 'screen -dmS foo java
-jar myapplication.jar' or 'gnome-terminal --execute java -jar
myapplication.jar', on Windows 'cmd /c start "foo" "java -jar
myapplication.jar"'). This has the (possibly desirable) side effect
of creating a visible/accessible terminal/console window for the newly
spawned application.

On Mar 1, 7:47 am, Ben Noordhuis <i...@bnoordhuis.nl> wrote:

mscdex

unread,
Mar 2, 2012, 3:44:31 PM3/2/12
to nodejs
On Mar 2, 3:28 pm, Jimb Esser <wastel...@gmail.com> wrote:
> The child_process module launches the children with piped stdout and
> stdin, and most processes will exit when they try to write to or read
> from a broken pipe (is your spawned app printing something to
> stdout?).  I don't know of any existing module to launch children
> unpiped, but it might not be too hard to make one.

In node's master branch, child_process's fork() supports a "silent"
option that does not share stdin/stdout/stderr with the child process.
Reply all
Reply to author
Forward
0 new messages