简体   繁体   English

Cron工作来运行PHP,即运行另一个PHP

[英]Cron job to run PHP, that runs another PHP

I have a PHP script that runs server side via a CRON job. 我有一个通过CRON作业运行服务器端的PHP脚本。 It functions just fine, but I need it to run every 10 seconds, not 60 seconds the limit of cron. 它的功能还不错,但是我需要它每10秒运行一次,而不是cron的上限60秒。 Can I write another "Timer.PHP", that would call/run the UPDATE.PHP, and have the Timer.php loop and sleep to give me the 10 second update time? 我可以编写另一个“ Timer.PHP”来调用/运行UPDATE.PHP,并使Timer.php循环并休眠以给我10秒钟的更新时间吗? I could then have the cron run the Timer.php every 2 minutes. 然后,我可以让cron每2分钟运行一次Timer.php。

The current cron job is: curl -s http://www.mysebsite.com/template/update/update.php?password=pass I am not sure of the following: 1) is this possible? 当前的cron作业是:curl -s http://www.mysebsite.com/template/update/update.php?password=pass我不确定以下内容:1)这可能吗? 2) The correct syntax? 2)正确的语法?

Someone suggested to have it loop 11 times (each loop 10 seconds apart) then stop until the cron calls it again. 有人建议让它循环11次(每个循环相隔10秒),然后停止直到cron再次调用它。 Their thoughts were to give it a 10 second down time to prevent the cron starting again before the previous one stopped its execution. 他们的想法是给它10秒钟的停机时间,以防止cron在上一个停止执行之前再次启动。

If you want to run it as a cron job, use php sleep() function. 如果要将其作为cron作业运行,请使用php sleep()函数。 Run a loop 5 times and sleep the script for 10 seconds after execution. 运行5次循环,执行后将脚本休眠10秒钟。 eg 例如

<?php

for ($x = 0; $x < 5; $x++) {
    // Execute your code
    sleep(10);
}

?>

This is not a perfect solution but it'll work fine. 这不是一个完美的解决方案,但可以正常工作。

After several failed attempts, I decided it will be simpler to edit the actual PHP file that the cron job is currently properly executing. 经过几次失败的尝试后,我决定编辑cron作业当前正在正确执行的实际PHP文件将更为简单。 I will figure out how to loop it 6 times with a 10 second sleep. 我将弄清楚如何以10秒的睡眠将其循环6次。 This will make it run 6 times with a 10 second delay. 这将使其运行6次,并有10秒的延迟。 The cron can then restart the script again. 然后,cron可以再次重新启动脚本。

Thank you all for trying to help yet another newbie Bob 谢谢大家尝试帮助另一个新手Bob

What I finally ended up doing is making 6 cron jobs, 5 have a sleep built into them. 我最终要做的是做6个Cron工作,其中5个有睡眠。

1) Normal run 2) Sleep 10; 1)正常运行2)睡眠10; 3) Sleep 20; 3)睡觉20; 4) Sleep 30; 4)睡眠30; 5) Sleep 40; 5)睡眠40; 6) Sleep 50; 6)睡眠50;

Fortunately my hosted server site allows this. 幸运的是,我的托管服务器站点允许这样做。
The script takes about 2 seconds to run. 该脚本大约需要2秒钟才能运行。 Been running this for over six hours so far with no errors. 到目前为止已经运行了六个多小时,没有错误。 May not be the most glamorous solution, but it works. 可能不是最迷人的解决方案,但它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM