I am creating a background process using proc_open()
and am wondering how to cancel or stop this background process, from a different file or action, while my background process is still running?
Here is my code :
$errorLog = './error.log';
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", $errorLog, "a") // stderr is a file to write to
);
proc_open("/var/bin/php /home/hello/myscript.php &", $descriptorspec, $pipes);
I already read about proc_terminate($process)
but it seems that requires the returned by proc_open()
to stop the process. Is there any way to stop it without having proc_open()
in the same script?
The typical way to do this is for the child script to get its process id (with getmypid()
in PHP) and write out a file (typically something like myscript.pid
) that contains this pid.
Then anything else that wants to terminate that script checks for the existence of the pid file, reads it, (and potentially for robustness, checks to see what the target process at that pid is), and then kills the target process. In PHP, you can do that with posix_kill()
. (Normally, you would send either SIGTERM
or SIGKILL
, but SIGHUP
, SIGINT
, or even SIGUSR1
or SIGUSR2
are appropriate, depending on what you want the child process to do when signalled.
Alternatively, the parent process can get the child's pid with proc_get_status()
and write the pid somewhere for later use. (This might be more appropriate if you need to run several instances of the child and/or the child can't be modified in this way.)
Also, important to note is that in your call to proc_open
, you're attempting to ask the shell to span the process in the background. This is entirely unnecessary. proc_open
does that itself, and by forcing the shell to be an intermediary, you're actually connected to the shell, not the process you're trying to launch. (This means that when your script exists, that will also cause the shell to kill the child, rather than the child continuing to run in the background.)