child_process.exec doesn't callback

1,927 views
Skip to first unread message

francisco treacy

unread,
May 27, 2010, 7:11:07 AM5/27/10
to nod...@googlegroups.com
I'm using child_process.exec to call system tasks.

exec("nohup node app/app.js &", sys.puts)

=> sys.puts never gets called (or at least i got bored of waiting =)

Whereas a simple example works just fine:

exec("date", sys.puts)

Does somebody know why does the latter work but not the former?

Francisco

David E. Brooks Jr

unread,
May 27, 2010, 10:45:54 AM5/27/10
to nod...@googlegroups.com

From the info page on 'nohup':

If standard output is a terminal, the command's standard output is
appended to the file `nohup.out'; if that cannot be written to, it is
appended to the file `$HOME/nohup.out'; and if that cannot be written
to, the command is not run.

-- Dave

Ryan Gahl

unread,
May 27, 2010, 9:40:14 AM5/27/10
to nod...@googlegroups.com
On Thu, May 27, 2010 at 6:11 AM, francisco treacy <francisc...@gmail.com> wrote:
I'm using child_process.exec to call system tasks.

exec("nohup node app/app.js &", sys.puts)

I'm no wizard, but my first guess would be the backgrounding is what the problem is. Does it do what you expect if you remove the ampersand?




 

Ryan Gahl

unread,
May 27, 2010, 12:33:15 PM5/27/10
to nod...@googlegroups.com
On Thu, May 27, 2010 at 9:45 AM, David E. Brooks Jr <dbro...@gmail.com> wrote:
If standard output is a terminal, the command's standard output is
appended to the file `nohup.out'; if that cannot be written to, it is
appended to the file `$HOME/nohup.out'; and if that cannot be written
to, the command is not run.

Ah. So he'd want something like (pseudo-concept, not sure how practical or correct but maybe a direction)...

exec("nohup node app/app.js &; tail -f /path/to/nohup.out", sys.puts);


francisco treacy

unread,
May 27, 2010, 2:56:14 PM5/27/10
to nod...@googlegroups.com
I don't think it's related to nohup.

I have tried with other commands (unicorn in daemon mode or a node
app, for example) and get the same results.

Consider this example, test.js:
var exec = require('child_process').exec
var sys = require('sys')
exec("node app/app.js &", sys.puts) // app.js is an express webapp

=> node test.js
(the callback doesn't get called - the prompt doesn't return - i have to ^C)


Strangely, if the webapp is already running i do get a return (btw NOT
in the error variable - weird)

=> node test.js
null

net:1033
bind(self.fd, arguments[0]);
^
Error: EADDRINUSE, Address already in use
at Server.listen (net:1033:28)
at [object Object].run
(/Users/ftreacy/.kiwi/current/seeds/express/0.11.0/lib/express/core.js:264:12)
at /Users/ftreacy/.kiwi/current/seeds/express/0.11.0/lib/express/dsl.js:76:29
at Object.<anonymous> (/Users/ftreacy/ws/app/app.js:145:1)
at Module._compile (module:384:23)
at Module._loadScriptSync (module:393:16)
at Module.loadSync (module:296:10)
at Object.runMain (module:459:22)
at node.js:168:8


2010/5/27 Ryan Gahl <ryan...@gmail.com>:

> --
> 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.
>

Matt Ranney

unread,
May 27, 2010, 3:01:04 PM5/27/10
to nod...@googlegroups.com
On Thu, May 27, 2010 at 4:11 AM, francisco treacy <francisc...@gmail.com> wrote:
I'm using child_process.exec to call system tasks.

exec("nohup node app/app.js &", sys.puts)

=> sys.puts never gets called (or at least i got bored of waiting =)

As soon as the child process exits, you'll get called though, right?

I think this is a feature, not a bug.  If you want to spawn things with node that detach themselves, the thing you spawn needs to exit.  node invokes the callback to exec after the child process signals its exit.


Ryan Gahl

unread,
May 27, 2010, 3:02:57 PM5/27/10
to nod...@googlegroups.com
On Thu, May 27, 2010 at 2:01 PM, Matt Ranney <m...@ranney.com> wrote:
I think this is a feature, not a bug.  If you want to spawn things with node that detach themselves, the thing you spawn needs to exit.  node invokes the callback to exec after the child process signals its exit.

Makes sense, and makes my attempt at help look a bit silly :)

francisco treacy

unread,
May 27, 2010, 3:21:03 PM5/27/10
to nod...@googlegroups.com
True, makes perfect sense.

If i kill the thing i spawn... then i get the callback.

How would you achieve this though (detach a process)?

I just want to fire a command and forget (i keep track of the process
through a pid file)


2010/5/27 Matt Ranney <m...@ranney.com>:

Matt Ranney

unread,
May 27, 2010, 4:11:47 PM5/27/10
to nod...@googlegroups.com
On Thu, May 27, 2010 at 12:21 PM, francisco treacy <francisc...@gmail.com> wrote:
How would you achieve this though (detach a process)?

Node doesn't give you this by default at the moment.  You could either extend node like this:


Or you could run some native code that does this for you like this:


I've never tried either, but they seem to do what you need.

francisco treacy

unread,
May 27, 2010, 5:02:02 PM5/27/10
to nod...@googlegroups.com
Many thanks.

This has enlightened me, I realize I was quite ignorant regarding daemons :)

Both look good, i'll be using daemonize shortly.

Francisco

2010/5/27 Matt Ranney <m...@ranney.com>:

Reply all
Reply to author
Forward
0 new messages