简体   繁体   English

每5分钟执行一次PHP cron,但15分钟后将函数置于睡眠状态以运行

[英]Execute a PHP cron every 5 minutes but put a function to sleep to run after 15 minutes

I have a PHP script that run on cron every 5 minutes. 我有一个PHP脚本,每5分钟在cron上运行一次。

*/5 * * * * /var/www/html/processMail.php

Inside the script there is a function that I want to execute every 15 minutes, so that function will be in sleep mode but the rest of the script will be process every 5 minutes. 脚本中有一个我想每15分钟执行一次的函数,因此该函数将处于睡眠模式,但脚本的其余部分将每5分钟执行一次。

does this crazy idea will work and how? 这个疯狂的想法会奏效吗?

does this crazy idea will work and how? 这个疯狂的想法会奏效吗?

No it won't. 不,不会。 The each 15 minute function would - even if you manage to wait 15 minutes - be executed each 5 minutes - so effectively the 15 minutes function is executed each 5 minutes. 即使您设法等待15分钟,每15分钟的功能也会每5分钟执行一次,因此实际上每5分钟执行15分钟的功能。 Instead create a second script that is executed every 15 minutes or add a parameter: 而是创建一个每15分钟执行一次的第二个脚本或添加一个参数:

*/5 * * * * /var/www/html/processMail.php --mins=5
*/15 * * * * /var/www/html/processMail.php --mins=15

Then use the functionality to parse commandline-parameters in PHP (and getopt ) to process accordingly. 然后使用该功能解析PHP中的命令行参数 (和getopt )以进行相应处理。 See as well the section about commandline usage in the PHP manual . 另请参见PHP手册中有关命令行用法的部分

You could save an iterator in a file or a database and increment it after every run, this way you can only run it once in 3 times the script runs. 您可以将迭代器保存在文件或数据库中,并在每次运行后递增它,这样,您只能在脚本运行3次中运行一次。

However, a much better solution would simply be to write another script and run it every 15 minutes. 但是,更好的解决方案将是编写另一个脚本并每15分钟运行一次。

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

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