一直在努力,现在推测这一个了几个小时。没运气。
我试图建立一个可以使用了shell_exec运行报告(在后台运行脚本)的系统。
下面的代码开始运行该报告的脚本:shell_exec("php /var/www/html/lab-40/test/invoice_reminder.php");
现在,我会怎样结束使用的PHP脚本执行?
我已经试过类似的事情,但PIDS我不知道我会如何去了解这一点。我在这里先向您的帮助表示感谢!
编辑:我不是想如果e.g关闭标签结束进程。
在此基础上comment了shell_exec帮助页面(&
将会使这一进程的背景下,和echo $!
将打印进程的PID):
<?php
function shell_exec_background(string $command): int {
return (int)shell_exec(
sprintf(
'nohup %s 1> /dev/null 2> /dev/null & echo $!',
$command
)
);
}
function is_pid_running(int $pid): bool {
exec(
sprintf(
'kill -0 %d 1> /dev/null 2> /dev/null',
$pid
),
$output,
$exit_code
);
return $exit_code === 0;
}
function kill_pid(int $pid): bool {
exec(
sprintf(
'kill -KILL %d 1> /dev/null 2> /dev/null',
$pid
),
$output,
$exit_code
);
return $exit_code === 0;
}
$pid = shell_exec_background('php /var/www/html/lab-40/test/invoice_reminder.php');
var_dump($pid);
var_dump(is_pid_running($pid));
var_dump(kill_pid($pid));
为了更好地控制子进程,看看PCNTL Functions和POSIX Functions。