如何运行cronjob每X分钟?

时间:2021-11-04 04:02:24

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)计划:

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("myemail@gmail.com", $subject, $message, "From: $from\n");
?>

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

但是我已经有30分钟没有收到这个配置的邮件了。

5 个解决方案

#1


75  

In a crontab file, 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 blah at 10 minutes past every hour.

意思是每小时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 cron executables that don't understand the */x notation.

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

If it still seems 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 cron daemon.

监控文件,确保每五分钟写一次。如果是,PHP脚本有问题。如果不是,您的cron守护进程有问题。

#2


20  

Your CRON should look like this:

你的CRON应该是这样的:

*/5 * * * *

*/5 * * * * *

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

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

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 /路径/到/脚本。php > /道路/ / logfile.log

#3


3  

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

你让你的cron每小时跑10分钟。将其设置为每5分钟更改为*/5 * * * * */ usr/bin/php / mydomainin . /cronmail。php > / dev / null 2 > & 1

#4


2  

2 steps to check if a cronjob is working :

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

  1. Login on the server with the user that execute the cronjob
  2. 与执行cronjob的用户一起登录服务器
  3. Manually run php command :

    手动运行php命令:

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

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

And check if any error is displayed

检查是否显示错误

#5


1  

# .---------------- 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 minuter 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

#1


75  

In a crontab file, 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 blah at 10 minutes past every hour.

意思是每小时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 cron executables that don't understand the */x notation.

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

If it still seems 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 cron daemon.

监控文件,确保每五分钟写一次。如果是,PHP脚本有问题。如果不是,您的cron守护进程有问题。

#2


20  

Your CRON should look like this:

你的CRON应该是这样的:

*/5 * * * *

*/5 * * * * *

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

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

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 /路径/到/脚本。php > /道路/ / logfile.log

#3


3  

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

你让你的cron每小时跑10分钟。将其设置为每5分钟更改为*/5 * * * * */ usr/bin/php / mydomainin . /cronmail。php > / dev / null 2 > & 1

#4


2  

2 steps to check if a cronjob is working :

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

  1. Login on the server with the user that execute the cronjob
  2. 与执行cronjob的用户一起登录服务器
  3. Manually run php command :

    手动运行php命令:

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

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

And check if any error is displayed

检查是否显示错误

#5


1  

# .---------------- 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 minuter 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