杀死用 popen() 打开的进程?

发布于 2024-10-13 01:28:14 字数 202 浏览 6 评论 0原文

我正在使用 popen() 打开一个长时间运行的进程。为了调试,我想在进程完成之前终止它。调用 pclose() 只会阻塞,直到子进程完成为止。

我怎样才能杀死该进程?我没有看到任何简单的方法可以从 popen() 返回的资源中获取 pid,以便我可以向它发送信号。

我想我可以做一些杂乱的事情,并尝试使用某种命令行黑客技术将 pid 混入输出中......

I'm opening a long-running process with popen(). For debugging, I'd like to terminate the process before it has completed. Calling pclose() just blocks until the child completes.

How can I kill the process? I don't see any easy way to get the pid out of the resource that popen() returns so that I can send it a signal.

I suppose I could do something kludgey and try to fudge the pid into the output using some sort of command-line hackery...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

反话 2024-10-20 01:28:14

好吧,找到了一个解决方案:我切换回 proc_open()而不是 popen()。那么就很简单了:

$s = proc_get_status($p);
posix_kill($s['pid'], SIGKILL);
proc_close($p);

Well, landed on a solution: I switched back to proc_open() instead of popen(). Then it's as simple as:

$s = proc_get_status($p);
posix_kill($s['pid'], SIGKILL);
proc_close($p);
め七分饶幸 2024-10-20 01:28:14

只需使用kill函数发送一个kill(或中止)信号:

Just send a kill (or abort) signal using kill function:

情绪少女 2024-10-20 01:28:14

您可以找到 pid,并通过执行以下操作检查您是否确实是其父级:

// 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);
}

它在 fast-cgi PHP 服务器上运行得很好。

You can find the pid, and checks that you're really its parent by doing:

// 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);
}

It's working quite well on a fast-cgi PHP server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文