Node.JS child processes being killed when parent dies
Asked Answered
P

1

17

I am using child_process.spawn() to start a script from my Node.JS application running on Ubuntu. As far as I know, standard forked or spawned *nix processes don't generally die when the parent dies, but when spawning processes from Node.JS, they seem to get killed when my application crashes, or is aborted with ctrl-c etc.

Why is this and is there a way around this? I can't seem to find any obvious option in the child_process API.

My application starts some quite long-running tasks that should run in the background, and if my node server crashes or is restarted for some other reason I don't want to interrupt the tasks, instead I want the node server to come back up and gracefully resume monitoring the progress of those running tasks.

Particle answered 8/6, 2015 at 12:8 Comment(0)
B
28

you need to set the detached option

If the detached option is set, the child process will be made the leader of a new process group. This makes it possible for the child to continue running after the parent exits.

var child = spawn('prg', [], {
   detached: true,
   stdio: [ 'ignore', out, err ]
 });
Bobstay answered 8/6, 2015 at 12:16 Comment(5)
Thanks a lot. Don't know HOW I managed to miss that. For reference, if anyone else finds this answer useful, here's from the manual: nodejs.org/api/…Particle
I have one concern though. The manual - and your code - specifically says that stdio must be directed elsewhere than to the parent process (which is default) for the process to continue in the background. However, I found that even if I didn't specify any stdio options, the process did infact continue even after I killed my node server. I am not sure what exactly is going on here, but maybe ['ignore', 'ignore', 'ignore'] is a wise option nonetheless.Particle
not specifying stdio will default to stdio: ['pipe', 'pipe', 'pipe']... which means the pipes are available through ChildProcess.stdin, ChildProcess.stdout and ChildProcess.stderr(in this case I think you must read from stdout and stderr otherwise your childprocess may hang)... if you don't need to interact with the childprosess's stdio from parent process, then you can just use 'ignore' which will forward any output from your childprocess to /dev/nullBobstay
Thanks for the additional info. Appreciated. I've got it working well now.Particle
But how would you then get actual data into the childprocess? If you use "ignore" for stdin, then the child.stdin property is undefined, and we can't write to it manually.Jawbone

© 2022 - 2024 — McMap. All rights reserved.