PHP脚本完成后将数据写入日志文件

时间:2015-01-16 08:59:31

标签: php

这是一个运行时间很长的脚本,即使用户关闭浏览器也需要运行。

<?php
ignore_user_abort(true); //run even when user disconnected.
set_time_limit(0); //run forever if that's what you need :)
$report = "";
while(1){
// do stuff and touch $report
}
//And the line below doesn't work when user disconnected.
file_put_contents("./offline.txt", $report, FILE_APPEND | LOCK_EX);
?>

我尝试过register_shutdown_function()但它似乎不起作用。
是否有任何快速解决方案或其他替代解决方案来处理任务?

2 个答案:

答案 0 :(得分:0)

如何创建另一个脚本并在后台运行呢?

You can take a look at the 3rd answer here

不要忘记在命令的末尾添加一个&符号(&amp;),以便它实际上在后台运行,例如

exec('php newscript.php > /dev/null &');

答案 1 :(得分:0)

也许您可以使用crontab来处理脚本。 您没有指定必须如何触发脚本 - 如果您运行它以回复用户行为 - 建议使用exec但如果您每天运行一次 - 请使用crontab < / p>