php 如何每X分钟运行一次cronjob?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25740573/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:58:44  来源:igfitidea点击:

How to run a cronjob every X minutes?

phpcroncron-task

提问by Savan Paun

I'm running a PHP script in a cronjob and I want to send emails every 5 minutes

我在 cronjob 中运行 PHP 脚本,我想每 5 分钟发送一次电子邮件

My current (crontab) cronjob:

我当前的(crontab)cronjob:

10 * * * * /usr/bin/php /mydomain.in/cromail.php > /dev/null 2>&1

The cronmail.php is as follows:

cronmail.php 如下:

<?php
$from = 'D'; // sender
$subject = 'S';
$message = 'M';
$message = wordwrap($message, 70);
mail("[email protected]", $subject, $message, "From: $from\n");
?>

But I've not received an email in 30 minutes with this configuration.

但是我在 30 分钟内没有收到有关此配置的电子邮件。

回答by paxdiablo

In a crontabfile, the fields are:

crontab文件中,字段是:

  • minute of the hour.
  • hour of the day.
  • day of the month.
  • month of the year.
  • day of the week.
  • 一小时的一分钟。
  • 一天中的小时。
  • 月中的一天。
  • 一年中的一个月。
  • 一周中的天。

So:

所以:

10 * * * * blah

means execute blahat 10 minutes past every hour.

表示blah每小时过去 10 分钟执行一次。

If you want every five minutes, use either:

如果您希望每五分钟一次,请使用:

*/5 * * * * blah

meaning every minute but only every fifth one, or:

意思是每分钟,但每五分之一,或:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * blah

for older cronexecutables that don't understand the */xnotation.

对于cron不理解*/x符号的旧可执行文件。

If it stillseems to be not working after that, change the command to something like:

如果此后似乎仍然无法正常工作,请将命令更改为:

date >>/tmp/debug_cron_pax.txt

and monitor that file to ensure something's being written every five minutes. If so, there's something wrong with your PHP scripts. If not, there's something wrong with your crondaemon.

并监视该文件以确保每五分钟写入一次内容。如果是这样,则您的 PHP 脚本有问题。如果没有,则您的cron守护进程有问题。

回答by Gary Jones

Your CRON should look like this:

你的 CRON 应该是这样的:

*/5 * * * *

*/5 * * * *

CronWTFis really usefull when you need to test out your CRON settings.

当您需要测试 CRON 设置时,CronWTF非常有用。

Might be a good idea to pipe the output into a log file so you can see if your script is throwing any errors too - since you wont see them in your terminal.

将输出通过管道传输到日志文件中可能是个好主意,这样您就可以查看脚本是否也抛出任何错误 - 因为您不会在终端中看到它们。

Also try using a shebang at the top of your PHP file, so the system knows where to find PHP. Such as:

还可以尝试在 PHP 文件的顶部使用 shebang,以便系统知道在哪里可以找到 PHP。如:

#!/usr/bin/php

#!/usr/bin/php

that way you can call the whole thing like this

这样你就可以这样称呼整个事情

*/5 * * * * php /path/to/script.php > /path/to/logfile.log

*/5 * * * * php /path/to/script.php > /path/to/logfile.log

回答by Justinas

You are setting your cron to run on 10th minute in every hour.
To set it to every 5 minschange to */5 * * * * /usr/bin/php /mydomain.in/cronmail.php > /dev/null 2>&1

您将 cron 设置为每小时运行第 10 分钟。
将其设置为every 5 mins更改为*/5 * * * * /usr/bin/php /mydomain.in/cronmail.php > /dev/null 2>&1

回答by kvantour

If you want to run a cron every nminutes, there are a few possible options depending on the value of n.

如果您想每n分钟运行一次 cron ,根据 的值,有几个可能的选项n

ndivides 60 (1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30)

n除以 60 (1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30)

Here, the solution is straightforward by making use of the /notation:

在这里,通过使用/符号,解决方案很简单:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *   command to be executed
m-59/n  *  *  *  *   command

In the above, nrepresents the value nand mrepresents a value smaller than nor *. This will execute the command at the minutes m,m+n,m+2n,...

上式中,n表示值nm表示小于n或的值*。这将在几分钟内执行命令m,m+n,m+2n,...

ndoes NOT divide 60

n不能除以 60

If ndoes not divide 60, you cannot do this cleanly with cron but it is possible. To do this you need to put a test in the cron where the test checks the time. This is best done when looking at the UNIX timestamp, the total seconds since 1970-01-01 00:00:00 UTC. Let's say we want to start to run the command the first time when Marty McFly arrived in Riverdale and then repeat it every nminutes later.

如果n不除以 60,则不能用 cron 干净利落地做到这一点,但这是可能的。为此,您需要在测试检查时间的 cron 中进行测试。最好在查看 UNIX 时间戳(自1970-01-01 00:00:00 UTC. 假设我们想在 Marty McFly 到达 Riverdale 时第一次开始运行命令,然后每n分钟重复一次。

% date -d '2015-10-21 07:28:00' +%s 
1445412480

For a cronjob to run every 42nd minute after `2015-10-21 07:28:00', the crontab would look like this:

对于42在“2015-10-21 07:28:00”之后每nd 分钟运行一次的 cronjob ,crontab 将如下所示:

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)
# |  |  |  |  |
# *  *  *  *  *   command to be executed
  *  *  *  *  *   minutetestcmd "2015-10-21 07:28:00" 42 && command

with minutetestcmddefined as

minutetestcmd定义为

#!/usr/bin/env bash
starttime=$(date -d "" "+%s")
# return UTC time
now=$(date "+%s")
# get the amount of minutes (using integer division to avoid lag)
minutes=$(( (now - starttime) / 60 ))
# set the modulo
modulo=
# do the test
(( now >= starttime )) && (( minutes % modulo == 0 ))

Remark:UNIX time is not influenced by leap seconds

备注:UNIX时间不受闰秒影响

Remark:cronhas no sub-second accuracy

备注:cron没有亚秒级精度

回答by Paul Giragossian

2 steps to check if a cronjob is working :

检查 cronjob 是否正常工作的 2 个步骤:

  1. Login on the server with the user that execute the cronjob
  2. Manually run php command :

    /usr/bin/php /mydomain.in/cromail.php

  1. 使用执行 cronjob 的用户登录服务器
  2. 手动运行 php 命令:

    /usr/bin/php /mydomain.in/cromail.php

And check if any error is displayed

并检查是否显示任何错误

回答by Javeed Shakeel

# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

To set for x minutes we need to set x minutes in the 1st argument and then the path of your script

要设置 x 分钟,我们需要在第一个参数中设置 x 分钟,然后设置脚本的路径

For 15 mins

15分钟

*/15 * * * *  /usr/bin/php /mydomain.in/cromail.php > /dev/null 2>&1