I am using this code on Ubuntu 13.04,
$cmd = "sleep 20 &> /dev/null &";
exec($cmd, $output);
Although it actually sits there for 20 seconds and waits :/ usually it works fine when using &
to send a process to the background, but on this machine php just won't do it :/
What could be causing this??
Try
I tested it for it works. Here you first fork the php process and then exceute your task.
Or if the pcntl module is not availabil use:
My workaround to do this on ubuntu 13.04 with Apache2 and any version of PHP:
libssh2-php
, I just usednohup $cmd &
inside a local SSH session using PHP and it ran it just fine the background, of course this requires putting certain security protocols in place, such as enabling SSH access for the webserver user, so it would have exec-like permissions then only allowing localhost to login to the webserver ssh account.The REASON this doesn't work is that
exec()
executes the string you're passing into it. Since&
is interpreted by the shell as "execute in the background", but you don't execute a shell in yourexec
call, the&
is just passed along with20
to the/bin/sleep
executable - which probably just ignores that.The same applies to the redirection of output, since that is also parsed by the shell, not in exec.
So, you either need to find a way to fork your process (as described above), or a way to run the subprocess as a shell.