0

There are several entries on this subject, for example here and enter, but none of the suggestion does seem to work. I have an example here:

$pid = shell_exec("nohup sleep 10 2> /dev/null & echo $!");
echo $pid;

which I expect to start a new command sleep 10 in the background, and return immediately to the shell. But the found behavior is that this code 'waits' for the execution of the subcommand sleep 10.

How can I run my commad in the background as a new process?

Community
  • 1
  • 1
Alex
  • 41,580
  • 88
  • 260
  • 469

1 Answers1

0

According to some notes to nohup on the wikipedia page, some shells deny to close unless all streams are free again.

A workaround therefore is to redirect all three standard streams: output, error and input, like in the following example:

nohup ./myprogram > foo.out 2> foo.err < /dev/null &

It is merely a guess, but that could probably be the case in your scenario.

$pid = shell_exec("nohup sleep 10 > /dev/null 2> /dev/null < /dev/null & echo $!");
echo $pid;

You might want to give this a try. I hope this is helpful even in case it does not answer your question.

hakre
  • 193,403
  • 52
  • 435
  • 836
  • @Alex: Okay, you deleted your comment: `$!` might return the PID from `nohup` because that is the command in the background. Did it solve now for you? – hakre Dec 13 '12 at 13:29