php 即使客户端或者服务器断开(如关掉浏览器)脚本也可以继续执行

<?php
ignore_user_abort(); // run script in background
set_time_limit(0); // run script forever
$interval=60*15; // do every 15 minutes...
    do{
       // add the script that has to be ran every 15 minutes here
       // ...
       sleep($interval); // wait 15 minutes
    }while(true);
?>
<?php
ignore_user_abort();           // 即使Client断开(如关掉浏览器),PHP脚本也可以继续执行.
set_time_limit(0);             // 执行时间为无限制,php默认的执行时间是30秒,通过set_time_limit(0)可以让程序无限制的执行下去
$interval=20;               // 时间间隔 单位 秒
$key_file="key.txt";          // 配置文件
if (isset($_GET['s']))
{
if ($_GET['s']=="0"){        // 停止工作,但不退出
    $s="false";
    echo "Function is off";
}
elseif ($_GET['s']=="1"){      // 工作
    $s="true";
    echo "Function is on";
}
elseif ($_GET['s']=="2"){      // 退出
    $s="die";
    echo "Function exited";
}
else
    die("Err 0:stop working 1:working 2:exit");

$string = "<?php\n return \"".$s."\";\n?>";
write_inc($key_file,$string,true);
exit();
}
if(file_exists($key_file)){
do{
    $mkey = include $key_file;
    if ($mkey=="true"){          // 如果工作
    / 工作区间 //
      $showtime=date("Y-m-d H:i:s"); 
      $fp = fopen('func.txt','a');
      fwrite($fp,$showtime."\n");
      fclose($fp);
    ///
    }
    elseif ($mkey=="die"){        // 如果退出
      die("I am dying!");
    }
    sleep($interval);           // 等待$interval分钟
}while(true);
}
else
die($key_file." doesn't exist !");
function write_inc($path,$strings,$type=false)
{   
$path=dirname(__FILE__)."/".$path;
if ($type==false)
    file_put_contents($path,$strings,FILE_APPEND);
else
    file_put_contents($path,$strings);
}
?>

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值