如何在UNIX服务器上在后台运行PHP代码
在我的网站上,我有一些功能可以上传用户的数据供稿.这些数据馈送通常具有30,000多个记录.每个记录都需要经过复杂的验证过程.因此,仅此提要处理将花费超过1个小时才能完成任务.这个长期运行的过程是不可避免的.
In my web site I have some feature to upload data feeds by users. These data feeds normally having more than 30,000 records. And each records need to be go through complex validation process. So simply this feeds processing will take more than 1 hour to complete the task. This long running process unavoidable.
此处,此供稿是由公共用户上传的.且Feed大小小于5MB的文本文件.因此,我们既不想授予FTP访问权限,也不想给那些用户提供命令行访问权限.只有我们更喜欢授予Web访问权限来上传提要.上传后,我们必须立即处理提要.处理后,我们还必须向他们发送邮件报告.
Here this feeds are uploaded by public user. And feed size is less than 5MB text file. So we don't want to give FTP access as well as we don't want to give command line access to those users. Only we prefer to give web access to upload the feeds. And we have to process the feed immediately after they upload. And once processed we have to send them a report of mail also.
现在的问题是,长时间运行的网页在许多网络上引发了代理超时问题(因为需要1个小时才能做出响应).这是大多数客户端网络配置的问题.
Now the problem is the long running webpages throwing some proxy time out issue on many net work (as it take 1 hour to respond). This is the problem of most client net work configuration.
提要上传后,我需要一种触发PHP脚本的方法.并且该脚本应在后台运行,同时正确完成页面加载,而不会延迟客户端.
I need a way to trigger a PHP script once the feed uploaded. And that script should run in background at the same time properly complete the page load without delaying the client.
在处理完成后,我将作为后台处理数据并将邮件发送给客户端. Cron标签在这里不可行,因为客户每月只能在少数情况下使用这些功能几次.
I will make the background to process the data and send mail to the client once the process completed. Cron tab not feasible here as client may use these feature only few times per month and in unexpected time range.
如果可以通过Web访问在后台触发PHP脚本的任何方式,请告诉我.
If there any way to trigger a PHP script in background but through web access please let me know.
用于简化要求的示例脚本
假设此示例,并帮助我修改此脚本以实现结果.我有两个PHP脚本"thread.php和createfile.php".客户端将通过Web浏览器打开名称为"thread.php"的文件.该脚本需要执行脚本"createfile.php".但是需要20秒才能回复.但是我们希望允许它在后台运行.并在浏览器中立即显示输出.
Assume this example and help me on modify this script to achieve the result. I have two PHP scripts "thread.php and createfile.php". The file with name "thread.php" will be opened by client through web browser. This script need to execute the script "createfile.php". But it will take 20 seconds to respond. But we want allow it run in background. And show an out put immediately in the browser.
以下是示例脚本上的代码.
Following are the code on the example scripts.
createfile.php ( http://sugunan.net/demo/createfile .php )
<?php
sleep(20);
@unlink("phpfile.txt");
$myfile = fopen("phpfile.txt", "w") or die("Unable to open file!");
$txt = date("h:i:s")."\n";
fwrite($myfile, $txt);
fclose($myfile);
mail("someone@example.com","Background process","Process completed");
?>
创建的文件可在以下网址找到: http://sugunan.net/demo/phpfile.txt
thread.php (( http://sugunan.net/demo/thread .php )
<pre>
<?php
echo "Script start at: " . date('h:i:s') . "\n";
include("createfile.php"); //give a method to execute this script but in background without delay
echo "Script end at: " . date('h:i:s');
?>
这将延迟sleep()
函数的输出.
脚本开始于:02:53: 27
脚本结束于:02:53: 47
Script end at: 02:53:47
没有include()
方法,有没有办法执行"createfile.php".并避免在后台处理"createfile.php"时出现20秒的sleep
延迟?
Is there any way to execute the "createfile.php" without include()
method. And avoid the 20 second sleep
delay while process the "createfile.php" in background?
请考虑我想避免在客户端等待的时间,并希望立即在浏览器中提供输出.
Please consider I want to avoid the waiting time on client side and want to give the out put on browser immediately.
At last I got a working answer from following post: php exec command (or similar) to not wait for result
因此,我按如下所示编辑代码以使我的要求正常工作.但是我不知道它是如何工作的.我只从该答案中复制语法,并在修改后将其粘贴到这里.
So I edit the code as follows to get my requirement working. But I don't know the explanation how it work. I only copy the syntax from that answer and past it here with modification.
<?php
echo "Script start at: " . date('h:i:s') . "\n";
exec('bash -c "exec nohup php createfile.php > /dev/null 2>&1 &"');
echo "Script end at: " . date('h:i:s');
?>
此处php createfile.php
将是执行PHP脚本并继续执行而无需等待输出的命令.
Here php createfile.php
will be the command to execute the PHP script and continue without waiting to output.
感谢克里斯蒂安