I have a bash script with a few lines similar to the following
echo "Do something"
/bin/sh -c 'echo $$>pidfile && exec "command"' &
echo "Ran Command">/path/to/outputfile.txt
exit 0
Then I call that from a PHP script
return shell_exec("/path/to/bash/script arguments");
Now, when I do that, the command is run successfully, and outputfile.txt contains "Ran Command".
However, the PHP script times out after 10ish seconds. The bash script takes about 2-3 seconds to run
If I change the line to
return shell_exec("/path/to/bash/script arguments >/dev/null 2>&1");
Then it executes and the PHP script doesn't time out.
I understand why redirecting the output lets PHP continue executing, but I can't figure out why PHP is timing out in the first place requiring me to do that. Can someone give me some assistance with this?
bin/shshould use2>&1 &instaed of&only, because you are moving the script into the background, but the outpu is still piped whitin the bash script. maybe PHP waits vor stdin/stderr to close./path/to/bash/script arguments? It doesn't. Only when I run that through php's shell_exec() does the time out happen.>/dev/null 2>&1 &look here unix.stackexchange.com/questions/70963/…the same timeoutmaybe you will not see an output, but the /bin/sh can still be running. check this withhtoporps -Af>/dev/nullpart, I knew you meant that. Is there any reason to put it in the bash script rather than the PHP script?