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

2022-01-03 00:00:00 cron cron-task php

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

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

我当前的(crontab)cronjob:

My current (crontab) cronjob:

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

cronmail.php 如下:

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
");
?>

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

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

推荐答案

crontab 文件中,字段是:

In a crontab file, the fields are:

  • 每小时的一分钟.
  • 一天中的几个小时.
  • 一个月中的第几天.
  • 一年中的一个月.
  • 星期几.

所以:

10 * * * * blah

表示每小时过去 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

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

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

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

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.

相关文章