How to Run a Cronjob Every X Minutes

How to run a cronjob every X minutes?

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.

Run Cron job every N minutes plus offset

To run a task every 20 minutes starting at 5 past the hour, try this:

 5-59/20 * * * *

Explanation

An * in the minute field is the same as 0-59/1 where 0-59 is the range and 1 is the step. The command will run at the first minute in the range (0), then at all successive minutes that are distant from the first by step (1), until the last (59).

Which is why */20 * * * * will run at 0 minutes, 20 minutes after, and 40 minutes after -- which is the same as every 20 minutes. However, */25 * * * * will run at 0 minutes, 25 minutes after, and 50 minutes after -- which is not the same as every 25 minutes. That's why it's usually desirable to use a step value in the minute field that divides evenly into 60.

So to offset the start time, specify the range explicitly and set the first value to the amount of the offset.

Examples

5-59/20 * * * * will run at 5 minutes after, 25 minutes after, and 45 minutes after.

10-59/25 * * * * will run at 10 minutes after and 35 minutes after.

1-59/2 * * * * will run every odd minute.

Cronjob every x minutes from 9 to 23 hours

The time range you define it is converted into:

9 50
10 40
11 30
12 20
13 10
14 00
15 50 -
16 40
17 30
18 20
19 10
20 00
20 50 -
21 40
22 30

So that you need to define these different cronjobs:

50 9,15,20 * * *
40 10,16,21 * * *
30 11,17,22 * * *
20 12,18 * * *
10 13,19 * * *
00 14,20 * * *

Run cron job every 2 hours In the X minute

Put your desired minute in the minute column, and then use */2 in the hour column to get it to run every two hours.

24 */2 * * * <your command>

How to setup CRON job to run every 10 minutes exactly from 9:24:59 to 15:14:59?

You can specify the time range for the script in the crontab file using bash syntax. For example see: Cron jobs and random times, within given hours and How to check if the current time is between 23:00 and 06:30.

Using bash commands in a single line can be difficult to understand and maintain. A simpler option is to use 3 cron tab entries. The first entry will run the script every 10 min from 09:25 to to 09:55. The second entry will run the script every 10 min from 10:05 to 14:55. The third entry will run the script from 15:05 to 15:15. These three crontab entries will cause the script to run at 09:25, 09:35, 09:45 .... 15:15. Following are the three crontab entries:

25-55/10 9 * * * script-path
5-55/10 10-14 * * * script-path
5-15/10 15 * * * script-path

You should confirm that the script runs at the correct times



Related Topics



Leave a reply



Submit