PHP - 杀死不同文件上的 proc_open() 进程? [英] PHP - Kill proc_open() process on different file?

查看:54
本文介绍了PHP - 杀死不同文件上的 proc_open() 进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 proc_open() 创建一个后台进程,我想知道如何在我的后台进程仍在运行时从不同的文件或操作中取消或停止这个后台进程?

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?

这是我的代码:

$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);

我已经阅读了有关 proc_terminate($process) 的内容,但似乎需要 proc_open() 返回的来停止进程.有没有办法在同一个脚本中没有 proc_open() 的情况下停止它?

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?

推荐答案

执行此操作的典型方法是让子脚本获取其进程 ID(使用 getmypid() 在 PHP 中)并写出一个文件(通常类似于 myscript.pid) 包含此 pid.

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.

然后,任何其他想要终止该脚本的程序都会检查 pid 文件是否存在,读取它(并且可能为了健壮性,检查该 pid 处的目标进程是什么),然后杀死目标进程.在 PHP 中,您可以使用 posix_kill().(通常,您会发送SIGTERMSIGKILL,但是SIGHUPSIGINT,甚至SIGUSR1SIGUSR2 是合适的,这取决于您希望子进程在收到信号时做什么.

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.

或者,父进程可以使用 获取子进程的 pidproc_get_status() 并将pid 写在某处以备后用.(如果您需要运行子项的多个实例和/或不能以这种方式修改子项,这可能更合适.)

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

另外,需要注意的是,在调用 proc_open 时,您试图要求 shell 在后台跨进程.这是完全没有必要的.proc_open 自己做这件事,通过强制 shell 作为中介,你实际上连接到 shell,而不是你试图启动的进程.(这意味着当你的脚本存在时,这也会导致 shell 杀死孩子,而不是孩子继续在后台运行.)

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

这篇关于PHP - 杀死不同文件上的 proc_open() 进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆