How to run a cronjob every X minutes?

Savan Paun picture Savan Paun · Sep 9, 2014 · Viewed 158.2k times · Source

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("[email protected]", $subject, $message, "From: $from\n");
?>

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

Answer

paxdiablo picture paxdiablo · Sep 9, 2014

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.