Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Cron job every N minutes plus offset

Tags:

cron

*/20 * * * *

Ensures it runs every 20 minutes, I'd like to run a task every 20 minutes, starting at 5 past the hour, is this possible with Cron? Would it be:

5/20 * * * * ?

like image 880
AJP Avatar asked Oct 08 '12 17:10

AJP


People also ask

How do I run a cron job after every minute?

Execute Cron Job After System Reboot. Setup and Run PHP Script As A Cron Job. Run crontab job every minute on a Linux or Unix-like system.

How do I schedule a script in crontab to run every 5 minutes?

basic 3. /usr/bin/vim. tiny 4. /bin/ed Choose 1-4 [1]: Make a new line at the bottom of this file and insert the following code. Of course, replace our example script with the command or script you wish to execute, but keep the */5 * * * * part as that is what tells cron to execute our job every 5 minutes.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .

Can I run cron job every minute?

“At every minute.” Cron job every 1 minute is a commonly used cron schedule. We created Cronitor because cron itself can't alert you if your jobs fail or never start.


2 Answers

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.

like image 58
toxalot Avatar answered Sep 23 '22 17:09

toxalot


Sure!

5,25,45 * * * * /your/cron 
like image 27
fedorqui 'SO stop harming' Avatar answered Sep 23 '22 17:09

fedorqui 'SO stop harming'