如何终止一个进程

发布于 2024-08-04 05:58:37 字数 186 浏览 4 评论 0原文

我正在一个 PHP 脚本中使用 proc_open 创建一个进程。

我如何在另一个脚本中终止它。我无法传递 proc_open 返回的资源。

我还尝试使用 proc_get_status() ,它返回 ppid 。我不知道孩子们的 pid 。

开发环境:WAMP

如有任何意见,我们将不胜感激。

I am creating a process using proc_open in one PHP script.

How do i terminate this in another script . I am not able to pass the resource returned by the proc_open.

I also tried using proc_get_status() , it returns the ppid . I don't get the pid of the children .

development env : WAMP

Any inputs is appreciated .

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

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

发布评论

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

评论(4

_蜘蛛 2024-08-11 05:58:37

我建议您重新检查您的模型,以确保您确实必须从其他地方终止该进程。除了最琐碎的情况之外,您的代码将变得越来越难以调试和维护。

为了保持其封装性,您可以向要终止的进程发出信号,并在要终止的进程中优雅地退出。否则,您可以使用普通 IPC 发送一条消息:“嘿,伙计。请关闭。”

编辑:对于第二段,您可能仍然最终启动一个脚本来执行此操作。没关系。你想要避免的是kill -9之类的事情。相反,让进程正常退出。

I recommend that you re-examine your model to make certain that you actually have to kill the process from somewhere else. Your code will get increasingly difficult to debug and maintain in all but the most trivial circumstances.

To keep it encapsulated, you can signal the process you wish to terminate and gracefully exit in the process you want to kill. Otherwise, you can use normal IPC to send a message that says: "hey, buddy. shut down, please."

edit: for the 2nd paragraph, you may still end up launching a script to do this. that's fine. what you want to avoid is a kill -9 type of thing. instead, let the process exit gracefully.

哑剧 2024-08-11 05:58:37

要在纯 PHP 中执行此操作,解决方案如下:

posix_kill($pid, 15); // SIGTERM = 15

To do that in pure PHP, here is the solution:

posix_kill($pid, 15); // SIGTERM = 15
z祗昰~ 2024-08-11 05:58:37

你最好使用这样的东西来启动你的其他进程:

$pid = shell_exec("nohup $Command > /dev/null 2>&1 & echo $!");

它将执行该进程,并为你提供一个正在运行的进程 ID。

exec("ps $pid", $pState);     
$running = (count($pState) >= 2);    

终止你总是可以使用

exec("kill $pid");

但是,你不能杀死不属于 PHP 运行用户的进程 - 如果它以无人身份运行 - 你将以无人身份启动新进程,并且只能杀死在无人用户下运行的进程。

You're best off using something like this to launch your other process:

$pid = shell_exec("nohup $Command > /dev/null 2>&1 & echo $!");

That there would execute the process, and give you a running process ID.

exec("ps $pid", $pState);     
$running = (count($pState) >= 2);    

to terminate you can always use

exec("kill $pid");

However, you cant kill processes not owned by the user PHP runs at - if it runs as nobody - you'll start the new process as nobody, and only be able to kill processes running under the user nobody.

拥抱我好吗 2024-08-11 05:58:37

您可以使用某种方法来创建进程,该方法通常返回新进程的PID。

这对你有用吗? :

$process = proc_open('php', $descriptorspec, $pipes, $cwd, $env);
$return_value = proc_close($process);

You can use some methond to create process, this method usually returns the PID of the new process.

Does this works for You? :

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