Killing processes opened with popen()?
我正在使用popen()打开一个长期运行的进程。对于调试,我想在完成之前终止该过程。调用pclose()只会阻塞直到子项完成。
如何终止该过程?我看不到任何简单的方法来从popen()返回的资源中获取pid,以便可以向其发送信号。
我想我可以做一些乱七八糟的事情,并尝试使用某种命令行黑客将pid欺骗到输出中...
好吧,找到了一个解决方案:我切换回
1 2 3 |
您可以找到该pid,并通过以下操作检查您是否确实是它的父对象:
1 2 3 4 5 6 7 | // Find child processes according to current pid $res = trim(exec('ps -eo pid,ppid |grep"'.getmypid().'" |head -n2 |tail -n1')); if (preg_match('~^(\\d+)\\s+(\\d+)$~', $res, $pid) !== 0 && (int) $pid[2] === getmypid()) { // I'm the parent PID, just send a KILL posix_kill((int) $pid[1], 9); } |
在快速cgi PHP服务器上运行良好。
只需使用kill函数发送一个kill(或中止)信号:
- php http://php.net/manual/zh/function.posix-kill.php
- c / c http://linux.die.net/man/3/kill