parik parik - 5 months ago 18
PHP Question

order of executing shell_exec in php

I want to execute 2 scripts:

s1
and
s2
.
s2
must be executed after
s1
.
The time of execution of
s1
is 2800 seconds, and I need to execute
s2
after
s1
. Should I put
sleep(3000)
in my php between them, or will the second one execute automatically after the first one?

I tried this:

exec('python s1 '); //time of execution is 2800s

sleep(3000); // i want to be sure that the first one is finished

shell_exec('php /home/Parik/s2.php');

Answer

From the exec manual page:

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

In short, PHP will wait for your python s1 command to finish before continuing.

Note that a execution time of 3000 seconds will most definitely time out without changing the default maximum execution time in the ini file. See How to increase maximum execution time in php.