[nodejs] Continuing background processing scripts

575 views
Skip to first unread message

Jonathan 'Wolf' Rentzsch

unread,
May 25, 2010, 2:29:08 AM5/25/10
to nod...@googlegroups.com
Hi all,

I'd like to spawn some ffmpeg processes upon completion of video file uploads, but I'd like the background processes to continue to completion even if I have to restart the node server parent process.

Sounds like I can't do this directly with child_process.spawn, so my current plan is to wrap my ffmpeg invocation in a ruby script like so:

#!/usr/bin/ruby
pid = fork do
`sleep 86400` # real work goes here
end
Process.detach(pid)

I suspect others have also crafted solutions for this. If so, I'd love to hear them.

-jwr

--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.

r...@tinyclouds.org

unread,
May 25, 2010, 2:47:09 AM5/25/10
to nod...@googlegroups.com
On Mon, May 24, 2010 at 11:29 PM, Jonathan 'Wolf' Rentzsch
<n...@redshed.net> wrote:
> Hi all,
>
> I'd like to spawn some ffmpeg processes upon completion of video file uploads, but I'd like the background processes to continue to completion even if I have to restart the node server parent process.
>
> Sounds like I can't do this directly with child_process.spawn, so my current plan is to wrap my ffmpeg invocation in a ruby script like so:
>
> #!/usr/bin/ruby
> pid = fork do
>   `sleep 86400` # real work goes here
> end
> Process.detach(pid)
>
> I suspect others have also crafted solutions for this. If so, I'd love to hear them.


I don't think your child processes will die if the server is terminated.

Isaac Schlueter

unread,
May 25, 2010, 3:25:09 AM5/25/10
to nod...@googlegroups.com
Is there a way to detach the child process?

Is this like doing "disown" in sh?

--i

Jonathan 'Wolf' Rentzsch

unread,
May 25, 2010, 12:03:38 PM5/25/10
to nod...@googlegroups.com
Hi Ryan,

Thanks for chiming in. If that's the case, then I don't understand the results of my test:

$ rlwrap node-repl
node> var spawn = require('child_process').spawn;
node> spawn('sleep',['86400']).pid
48165
node>^C
$ ps auxww|grep 48165
wolf 48167 0.0 0.0 2435036 524 s007 R+ 11:01AM 0:00.00 grep 48165

What am I missing?

-jwr

Chris Winberry

unread,
May 25, 2010, 1:26:26 PM5/25/10
to nod...@googlegroups.com

Are you sure? I do not use any signal trapping for clean up yet when I terminate a main process all the child processes go with it.

On May 25, 2010 2:47 AM, <r...@tinyclouds.org> wrote:

On Mon, May 24, 2010 at 11:29 PM, Jonathan 'Wolf' Rentzsch
<n...@redshed.net> wrote:
> Hi all,
>

> I'...

I don't think your child processes will die if the server is terminated.


--
You received this message because you are subscribed to the Google Groups "nodejs" group.

To po...

r...@tinyclouds.org

unread,
May 25, 2010, 1:48:34 PM5/25/10
to nod...@googlegroups.com
On Tue, May 25, 2010 at 9:03 AM, Jonathan 'Wolf' Rentzsch
<n...@redshed.net> wrote:
> Hi Ryan,
>
> Thanks for chiming in. If that's the case, then I don't understand the results of my test:
>
> $ rlwrap node-repl
> node> var spawn = require('child_process').spawn;
> node> spawn('sleep',['86400']).pid
> 48165
> node>^C
> $ ps auxww|grep 48165
> wolf     48167   0.0  0.0  2435036    524 s007  R+   11:01AM   0:00.00 grep 48165
>
> What am I missing?

Sorry, what I meant is that Node doesn't explicitly kill its children
on exit. Reparenting does have to be done if the child cannot itself
ignore SIGINT. I suppose something like you've shown is required.
Should node include a spawnOrphan() command?

Isaac Schlueter

unread,
May 25, 2010, 1:53:42 PM5/25/10
to nod...@googlegroups.com
It'd be awesome to have an API with child.detach(), child.attach(),
and a boolean "child.attached" that starts off as true.

var child = require("child_process").spawn("sleep", ["86400"])
child.detach()
process.exit(0) // lazy child sleeps all day

--i

r...@tinyclouds.org

unread,
May 25, 2010, 2:10:43 PM5/25/10
to nod...@googlegroups.com
On Tue, May 25, 2010 at 10:53 AM, Isaac Schlueter <i...@izs.me> wrote:
> It'd be awesome to have an API with child.detach(), child.attach(),
> and a boolean "child.attached" that starts off as true.
>
> var child = require("child_process").spawn("sleep", ["86400"])
> child.detach()
> process.exit(0) // lazy child sleeps all day

How would that work? setpgrp(child.pid, 1)? (Does that work, is that portable?)

Isaac Schlueter

unread,
May 25, 2010, 2:25:52 PM5/25/10
to nod...@googlegroups.com
On Tue, May 25, 2010 at 11:10, <r...@tinyclouds.org> wrote:
> On Tue, May 25, 2010 at 10:53 AM, Isaac Schlueter <i...@izs.me> wrote:
>> It'd be awesome to have an API with child.detach(), child.attach(),
>> and a boolean "child.attached" that starts off as true.
>>
>> var child = require("child_process").spawn("sleep", ["86400"])
>> child.detach()
>> process.exit(0) // lazy child sleeps all day
>
> How would that work?

I dunno. I kinda figured that you'd write some magic C code, and then
it'd do exactly what I want. That's how node's been working so far.
:)

How does the "disown" command do it? I'd like to be able to log into
a machine, start a node program running (as a child process or
whatever), and then log out, and have it keep going.

--i

Scott González

unread,
May 25, 2010, 2:34:09 PM5/25/10
to nod...@googlegroups.com
On Tue, May 25, 2010 at 2:25 PM, Isaac Schlueter <i...@izs.me> wrote:
How does the "disown" command do it?  I'd like to be able to log into
a machine, start a node program running (as a child process or
whatever), and then log out, and have it keep going.

If all you want to do is start a node process that will keep running after you log out, you can use nohup.

nohup node process.js

Isaac Schlueter

unread,
May 25, 2010, 2:51:01 PM5/25/10
to nod...@googlegroups.com
2010/5/25 Scott González <scott.g...@gmail.com>:

> On Tue, May 25, 2010 at 2:25 PM, Isaac Schlueter <i...@izs.me> wrote:
>>
>> How does the "disown" command do it?  I'd like to be able to log into
>> a machine, start a node program running (as a child process or
>> whatever), and then log out, and have it keep going.
>
> If all you want to do is start a node process that will keep running after
> you log out, you can use nohup.
>
> nohup node process.js

Yep. Or background it and then disown. If that's *all* you wanted to
do, that'd be great.

But what I actually want is to see what creative things people end up
doing when they have access to this functionality in JavaScript. How
would you go about writing a nohup clone as a node program? What
other interesting things could be done with a long-lived background
process?

--i

Matt Ranney

unread,
May 25, 2010, 5:49:43 PM5/25/10
to nod...@googlegroups.com
On Tue, May 25, 2010 at 11:51 AM, Isaac Schlueter <i...@izs.me> wrote:
But what I actually want is to see what creative things people end up
doing when they have access to this functionality in JavaScript.  How
would you go about writing a nohup clone as a node program?  What
other interesting things could be done with a long-lived background
process?

If all you want is nohup, the source is very, very simple:


There's really only one line that matters to most people:

(void)signal(SIGHUP, SIG_IGN);



Reply all
Reply to author
Forward
0 new messages