Given the following php code...
<?php
exec('myscript.php');
exec('finish.php');
?>
Is there a way to tell when myscript.php has stopped running?
I'd like to prevent finish.php
from running until myscript.php
is completed.
The exec() calls will block until the external job has completed. Unless you force one of the myscript call to run in the background, you'll never have finish.php running while myscript.php is.
$output = shell_exec("myscript.php");
This is just a guess, but i'm willing to bet you'll see execution errors. This is likely because myscript.php does not have the needed bang line (#!/usr/bin/php) and +x flag to run.
* UPDATE *
Alternatively, it seems like you can just
include("myscript.php");
include("finish.php");
This will have the result you're looking for. If you actually want to execute these outside of the running process, see my above comment about bang line and making the scripts executable.
Is there a way to tell when myscript.php has stopped running?
If it runs finish.php
the execution of myscript.php
is finished. If it is a background process you could for example do something 'hacky' like creating a file when myscript.php
is finished and check for that.
From the docs:
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.
I would recommend you to use proc_open as it provides much more flexibility. You should also note that in the case of exec(), the second one will only be called after the first one terminates.