php在后台运行,完成后退出

发布于 2024-12-23 12:13:27 字数 245 浏览 3 评论 0原文

<代码> $escaped_check = escapeshellcmd("/usr/bin/php -f /opt/status/check.php " . $_SERVER['REMOTE_ADDR'] . " >> /dev/null 2>&1 &") ; shell_exec($escaped_check);

为了非阻塞线程,我试图在后台执行上述代码,但我不确定工作完成后如何退出 check.php。


$escaped_check = escapeshellcmd("/usr/bin/php -f /opt/status/check.php " . $_SERVER['REMOTE_ADDR'] . " >> /dev/null 2>&1 &");
shell_exec($escaped_check);

I am trying to execute the above code in the background for the sake of non-blocking thread, but I am not sure how to exit the check.php when the job is done.

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

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

发布评论

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

评论(2

箜明 2024-12-30 12:13:28

您可以使用 popen 代替 shell_exec。

然后你可以通过proc_terminate终止它

,或者你可以通过调用proc_get_status获取进程id。

通过 PID,您还可以终止进程。

我希望这有帮助。

you can popen instead of shell_exec.

Then you can terminate it by proc_terminate

Or you can obtain process id by calling proc_get_status.

With PID you can terminate process also.

I hope this helps.

呆° 2024-12-30 12:13:28

PHP 脚本(您的 check.php)在后台运行完成后应自动终止。
您可能可以尝试在 PHP 脚本(您的 check.php)末尾使用 exit() 或 die() 以确保脚本确实跳出。

您还可以使用以下命令来获取 $pid。例如,23456。“echo $!”部分发送进程 ID。

$pid = shell_exec('php /path/to/script/cli_test.php argument1 > /dev/null 2>&1 & echo $!') 

然后在终端中使用 ps 命令检查它是否仍在后台运行。

ps 23456

The PHP script (your check.php) should be terminated automatically after it finishes running at background.
You probably can try using exit() or die() at the end of the PHP script (your check.php) to make sure the script does jump out .

You can also use the following command to get the $pid. For example, 23456. The "echo $!" part sends the process id.

$pid = shell_exec('php /path/to/script/cli_test.php argument1 > /dev/null 2>&1 & echo $!') 

And then in the terminal, use ps command to check if it is still running at background.

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