我有一个可用的 php 脚本,使用 shell_exec 来执行一个外部程序,大约需要 30 秒才能完成。
问题是,如果用户关闭浏览器或由于某种原因关闭连接,则使用 shell_exec 执行的程序将继续运行,因为它的输出无法再发送给用户。
有没有办法在连接关闭后立即终止该进程?
感谢您的帮助。
终于我解决了!
我用过PHP函数connection_aborted()
检查客户端是否断开连接。https://www.php.net/manual/en/function.connection-aborted.php https://www.php.net/manual/en/function.connection-aborted.php
算法如下(伪代码)
shell_exec("script");//script is a shell script that launch the program in another thread thus this return immediately
$task_completed=false;
while(!$task_completed){
if(connection_aborted()==1){
kill_the_process();//this will kill the running process launched by script
die();
}
$task_completed=...;//true if the task completed
sleep(1);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)