脚本宝典收集整理的这篇文章主要介绍了PHP – 在客户端断开连接时杀死exec,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
感谢您的任何帮助或建议.
这会导致PHP在生成内容时将输出刷新到客户端,这允许PHP检测客户端何时断开连接.当客户端立即断开连接并终止子进程时,PHP将终止(只要未设置ignore_user_abort()).
例:
<?PHP function exec_unix_bg ($cmd) { // Executes $cmd in the background and returns the PID as an integer return (int) exec("$cmd > /dev/null 2>&1 & echo $!"); } function pid_exists ($pid) { // Checks whether a PRocess wITh ID $pid is running // There is probably a better way to do this return (bool) trim(exec("ps | grep \"^$pid \"")); } $cmd = "/path/to/your/cpp arg_1 arg_2 arg_n"; // Start the C++ program $pid = exec_unix_bg($cmd); // Ignore user aborts to allow us to dispatch a signal to the child ignore_user_abort(1); // Loop until the program completes while (pid_exists($pid)) { // Push some harMLess data to the client echo " "; flush(); // Check whether the client has disconnected if (connection_aborted()) { posix_kill($pid,SIGTERM); // Or SIGKILL,or whatever exit; } // Could be done better? Only here to prevent runaway cpu sleep(1); } // The process has finished. Do your thang here.
要收集程序的输出,请将输出重定向到文件而不是/ dev / null.我怀疑你需要安装pcntl以及posix,因为PHP手册表明SIGxxx常量是由pcntl扩展定义的 – 尽管我没有安装过没有其他的,所以我不确定.
以上是脚本宝典为你收集整理的PHP – 在客户端断开连接时杀死exec全部内容,希望文章能够帮你解决PHP – 在客户端断开连接时杀死exec所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。