PHP - Kill proc_open() process on different file?

自闭症网瘾萝莉.ら 提交于 2020-01-04 09:19:18

问题


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?


回答1:


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.)



来源:https://stackoverflow.com/questions/35071896/php-kill-proc-open-process-on-different-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!