3

I am testing a php script that has been developed on an OS-X system at Debian and it behaves different there.

To reproduce it I wrote two scripts: parent.php and child.php:

parent.php:

#!/usr/bin/php
<?php

echo "parent started...\n";
shell_exec(__DIR__ . '/child.php &2>/dev/null &');
echo "parent finished.\n";

child.php:

#!/usr/bin/php
<?php

echo "child started...\n";
sleep(5);
echo "child finished.\n";

Running parent.php on OS-X I get back imediately the two output lines (parent started, parent finished). On Debian I get the "parent started..." line, then a delay of 5 seconds an then the "parent finished.". Running "./child.php &2>/dev/null &" in the shell gives me back the prompt imediately as expected. Any ideas how I can fix this?

2
  • 2
    While I'm sure it's just a shorthand I've never seen before, since you say it works, I usually write that exec line as {cmd} > /dev/null 2>&1 & - would be interested to know if that makes a difference. Commented Jan 17, 2012 at 17:12
  • Did you mean to suppress errors by &2>/dev/null or just want to suppress any output? Commented Jan 17, 2012 at 17:18

2 Answers 2

4

This is because &2> part. It may not be supported in all systems. Also in every shell (bash, sh, ksh etc).

Try this,

 exec("/bin/bash -c '/usr/bin/php /path/to/child.php 2> /dev/null' &");

If you want to suppress all the output use this,

 exec("/bin/bash -c '/usr/bin/php /path/to/child.php &> /dev/null ' &");

BASH-HOWTO

Just tested, exec("/usr/bin/php /path/to/child.php > /dev/null 2>&1 &") should work too.

Sign up to request clarification or add additional context in comments.

2 Comments

Your last line is missing the & on the end to separate the child process from the parent
@dave Thanks. Missed it as I typed too quickly.
2

Try with exec() or system() instead of shell_exec, maybe shell_exec has not the same behavior on different OS.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.