Linux 如何每分钟更频繁地运行 Cronjobs?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4672383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run Cronjobs more often than once per minute?
提问by Norwald2
I created a Email-Queue database table. I will insert all Emails my PHP application needs to send into this table.
我创建了一个电子邮件队列数据库表。我将把我的 PHP 应用程序需要发送的所有电子邮件插入到这个表中。
Another PHP script will then look for all unsent Emails and sends them.
另一个 PHP 脚本将查找所有未发送的电子邮件并发送它们。
I run this script using cronjobs. Unfortunately cronjobs can run only at a maximum of once per minute. So in the worst-case a user has to wait one minute until his Email is really going to be sent.
我使用 cronjobs 运行此脚本。不幸的是,cronjobs 最多只能每分钟运行一次。所以在最坏的情况下,用户必须等待一分钟,直到他的电子邮件真正被发送。
My current idea for a workaround is calling the script with an addtional sleep parameter and duplicating the cronjobs.
我目前的解决方法是使用附加睡眠参数调用脚本并复制 cronjobs。
Example:
例子:
* * * * * curl emails.php?sleep=0 >/dev/null 2>&1
* * * * * curl emails.php?sleep=10 >/dev/null 2>&1
* * * * * curl emails.php?sleep=20 >/dev/null 2>&1
* * * * * curl emails.php?sleep=30 >/dev/null 2>&1
* * * * * curl emails.php?sleep=40 >/dev/null 2>&1
* * * * * curl emails.php?sleep=50 >/dev/null 2>&1
In the above example the script would run every 10 seconds. The first line of the emails.php Script would be:
在上面的示例中,脚本将每 10 秒运行一次。emails.php 脚本的第一行是:
sleep($_REQUEST['sleep']);
采纳答案by John Parker
For starters, I'd recommend using the command line version of PHP rather than using curl to call a script. You can then create a PHP script with a sensible lifetime that isn't constrained by having to output a response within a given time period.
对于初学者,我建议使用 PHP 的命令行版本,而不是使用 curl 来调用脚本。然后,您可以创建一个具有合理生命周期的 PHP 脚本,该脚本不受必须在给定时间段内输出响应的限制。
As such, you can then simply sleep/check for emails/transmit/sleep, etc. within the PHP script rather than needlessly using cron.
因此,您可以在 PHP 脚本中简单地睡眠/检查电子邮件/传输/睡眠等,而不是不必要地使用 cron。
Additionally, I'd take care to ensure that multiple PHP scripts aren't operating on the database table at the same time, either by using a pid file or database setting approach (if a given file/setting exists/is set, abort processing) or by sensibly timing the cron job and limiting the maximum processing time of the script by checking how long it's been running for prior to beginning the "check for emails" portion of the cycle.
此外,我会注意确保多个 PHP 脚本不会同时在数据库表上运行,无论是使用 pid 文件还是数据库设置方法(如果给定的文件/设置存在/设置,则中止处理) 或者通过在开始周期的“检查电子邮件”部分之前检查脚本运行了多长时间来合理地计时 cron 作业并限制脚本的最大处理时间。
回答by Ish
This has to be done at script level.
这必须在脚本级别完成。
// cron.php running every 10 seconds
<?php
$expireTime = time() + 60;
while (time() < $expireTime) {
// my php logic here
sleep(10);
// sleep for 10 seconds
// you may change the sleep time to change frequency
}
回答by ypnos
If you do this every 10 seconds, cron is just not the right tool for the job. What you need is a script that runs continuously as shown by Ish.
如果您每 10 秒执行一次,cron 就不是完成这项工作的正确工具。您需要的是一个连续运行的脚本,如 Ish 所示。
You can register the script as a service by putting an according start/stop script into /etc/init.d and enable it.
您可以通过将相应的启动/停止脚本放入 /etc/init.d 并启用它来将脚本注册为服务。
Or if you only have a user account, you can run it in a screen session. Then you can even watch the output for error diagnosis.
或者,如果您只有一个用户帐户,则可以在屏幕会话中运行它。然后您甚至可以查看输出以进行错误诊断。
回答by shlomia
Here's a simple bash script I've written which can be used with crontab to run more frequently than 1 minute.
这是我编写的一个简单的 bash 脚本,它可以与 crontab 一起使用以超过 1 分钟的频率运行。
you can save it as ~/bin/runEvery.sh and then in crontab write something like this to run otherScript.sh every 5 seconds:
你可以将它保存为 ~/bin/runEvery.sh 然后在 crontab 中写这样的东西来每 5 秒运行一次 otherScript.sh:
*/1 * * * * ~/bin/runEvery.sh 5 otherScript.sh
*/1 * * * * ~/bin/runEvery.sh 5 otherScript.sh
This is the script:
这是脚本:
#!/bin/bash
inputPeriod=
runCommand=
RUN_TIME=60
error="no"
if [ 'x'"$runCommand" != 'x' ]
then
if [ 'x'$inputPeriod != 'x' ]
then
loops=$(( $RUN_TIME / $inputPeriod ))
if [ $loops -eq 0 ]
then
loops=1
fi
for i in $(eval echo {1..$loops})
do
$runCommand
sleep $inputPeriod
done
else
error="yes"
fi
else
error="yes"
fi
if [ $error = "yes" ]
then
echo "runEvery - runs a command every X seconds for a minute"
echo "Usage: runEvery.sh <# in seconds < 60> <command to run>"
fi