PHP脚本重新运行自己,直到进程完成.然后每周重新启动


PHP Script to re-run itself until process completed.and then restart every week

我制作了一个从web服务检索XML内容的脚本。这个过程需要每周运行一次,但脚本本身需要重新运行大约180次才能完成这个过程。脚本的每次运行大约需要3-8分钟。我希望它在每次完成后重新运行大约5秒。

我目前的解决方案是:

  • windows的任务调度程序每周打开一次php页面
  • 当脚本运行并完成时,javascript会在完成后5秒重新启动页面
  • 当脚本最后一次运行时,它会删除页面的重新加载,从而停止

这个解决方案的问题是,它每周都会打开一个新的浏览器窗口。有什么好的替代方法可以在不必手动关闭浏览器的情况下做到这一点吗?

重新运行脚本的原因是由于php服务器最大限制的脚本超时设置,以及每次运行后查看状态是否发生任何错误的可能性。

我不使用cron,因为它需要进行大量的民意调查,才能在上次运行后的5秒内启动该过程。对于脚本的每周启动,我认为只要脚本使用javascript重新运行它自己,它就不会工作?

使用PHP:

<?php
// increase the maximum execution time to 43200 seconds (12 hours)
set_time_limit(43200);
function runTask() {
    static $cycles = 0;
    // do whatever you need to do
    // Increments cycle count then compares against limit
    if ($cycles++ < 180)  {
        sleep(5);  // wait five seconds
        runTask(); // run it again
    }
}
runTask(); // fire up the loop

或者,如果你是Javascript的粉丝。。。

使用node.js:

var cycles = 0;
function runTask() {
  // do whatever you need to do
  // Increments cycle count then compares against limit
  if (cycles++ < 180) {
    setTimeout(runTask, 5000); // run again in 5000 milliseconds
  }
}
runTask(); // fire up the loop

两种解决方案在每次迭代完成5秒后才会再次运行函数。

只需让您的任务运行器直接执行任一脚本;不需要浏览器。