如何每X分钟运行一个cronjob? [英] How to run a cronjob every X minutes?
问题描述
我在cronjob中运行PHP脚本,我想每5分钟发送一次电子邮件
我当前的(crontab)cronjob:
10 * * * * / usr / bin / php /mydomain.in/cromail.php> / dev / null 2>& 1
cronmail.php如下:
<?php
$ from ='D'; // sender
$ subject ='S';
$ message ='M';
$ message = wordwrap($ message,70);
mail(myemail@gmail.com,$ subject,$ message,From:$ from\\\
);
?>
但是我在这个配置的30分钟内没有收到电子邮件。
在 crontab
文件中,字段为:
- 每天的小时。
所以:
10 * * * * blah
pre>
表示在每小时10分钟后执行
blah
。
如果你想每五分钟,使用:
* / 5 * * * * blah
意味着每分钟但每五分之一,或:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * blah
$
/ pre>
/ code>符号。
如果仍然似乎无法正常工作,请将命令更改为:
日期>> /tmp/debug_cron_pax.txt
并监视该文件,以确保每五分钟写一次。如果是这样,你的PHP脚本有问题。如果没有,您的
cron
守护程序有问题。I'm running a PHP script in a cronjob and I want to send emails every 5 minutes
My current (crontab) cronjob:
10 * * * * /usr/bin/php /mydomain.in/cromail.php > /dev/null 2>&1
The cronmail.php is as follows:
<?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.
解决方案In a
crontab
file, the fields are:
- 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.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.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.这篇关于如何每X分钟运行一个cronjob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!