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

PHP脚本完成后将数据写入日志文件,php,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 w

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

<?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);
?>

创建另一个脚本并在后台运行怎么样

不要忘记在命令末尾加上一个符号(&),这样它就可以在后台运行,例如

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

也许您可以使用
crontab
来处理脚本。 您没有指定必须如何启动脚本-如果您运行脚本以响应用户行为-建议使用
exec
,但如果您每天运行一次,则使用
crontab