I'm aware that we can run background process with the following:
exec("doTask.php $arg1 $arg2 $arg3 >/dev/null 2>&1 &");
(Taken from: Run PHP Task Asynchronously)
However I have a script that depends on the status of the first execution. Example:
mongoexport ... | awk ... > my.csv && zip myzip my.csv
The CSV file must exists before it can zip.
Update: filename and a few query conditions are user submitted values.
So I have something like:
exec("mongoexport ... | awk ... > my.csv && zip myzip my.csv > /dev/null 2>&1 &);
But that doesn't work. PHP hangs there waiting for the script to finish running.
I tried
exec("mongoexport ... | awk ... > my.csv &);
That works.
So is &&
the reason it doesn't run in background?
How can I make the example run in background?
The following will work
exec("(mongoexport ... | awk ... > my.csv && zip myzip my.csv) > /dev/null 2>&1 &")
It tells the shell to run all the sequence of commands enclosed in braces in the background process and redirect the output, so the exec
function has nothing to wait for.
In your example, the shell launched from exec
waited for the awk
to finish and after that ran zip
in background (if awk exited with status 0)