![](/img/trans.png)
[英]use exec to run a process in the background and still write the output to a log file
[英]redirect but still continue to process exec()
此代码示例应该重定向并继续处理长操作,但是在exec
命令完成之后它才会重定向。 我已经尝试了多种其他方法来做到这一点并没有任何效果。 我哪里错了?
我的php.ini中启用了ignore_user_abort()
<?php
set_time_limit ( 0 );
header ( 'Connection: close' );
ob_start ();
header ( 'Content-Length: 0' );
header ( 'Location: /redirect.php' );
ob_end_flush ();
flush ();
ignore_user_abort ( true );
exec('command that takes 5 minutes to process');
exit ();
?>
我提前感谢你的帮助。
我最近不得不写一个webhook处理程序,它应该在继续处理其他长时间运行的任务之前立即返回“200 OK”响应。
我观察到,当我使用相同的Web服务器进行整个过程时,立即挂起的过程是不可能的。 因此,如果您需要发送响应的连接的客户端IP与服务器IP相同,则可能永远无法执行此操作。 但是,当传入的连接是真实世界的远程客户端时,它始终有效。
尽管如此,我只是回复了“200 OK”的回复。 这与发送重定向不同。
对我有用的代码是......
public function respondASAP($responseCode = 200)
{
// check if fastcgi_finish_request is callable
if (is_callable('fastcgi_finish_request')) {
/*
* This works in Nginx but the next approach not
*/
session_write_close();
fastcgi_finish_request();
return;
}
ignore_user_abort(true);
ob_start();
http_response_code($responseCode);
header('Content-Encoding: none');
header('Content-Length: '.ob_get_length());
header('Connection: close');
ob_end_flush();
ob_flush();
flush();
}
您可以根据您的情况调整它,并使其设置重定向标题而不是“200 OK”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.