Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do set cron to run my script every 40mins/25mins?

Tags:

I want a script to run every 40mins beginning on the 40th minute.
so that means:

00:40, 01:20, 02:00, 02:40, 03:20...

So I made this entry to cron:

*/40 * * * * /path/to/script/foo.sh

Unfortunately this runs the script every 40th minute of the hour:

00:40, 01:40, 02:40...

The same goes with the script that I meant to run every 25mins.

Am I missing something here?


ANSWERS
Alright, in case you happen to drop by here having the same problem
here's how I solved it:

# 40mins-interval
40 0 * * * /path/foo.sh         (0)
0,40 2-22/2 * * * /path/foo.sh  (2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22)
20 1-23/2 * * * /path/foo.sh    (1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23)  


# 25mins-interval
25,50 0 * * * /path/foo.sh              (0)
0,25,50 5-20/5 * * * /path/foo.sh       (5, 10, 15, 20)
15,40 1-21/5 * * * /path/foo.sh         (1, 6, 11, 16, 21)
5,30,55 2-22/5 * * * /path/foo.sh       (2, 7, 12, 17, 22)
20,45 3-23/5 * * * /path/foo.sh         (3, 8, 13, 18, 23)
10,35 4-19/5 * * * /path/foo.sh         (4, 9, 14, 19)

Notes:
1. There will still be collisions in this schedule (i.e: see schedules that run on the 0th and 10th minutes on both intervals).
2. The script won't run at an exact interval from its last run today going on the next day (i.e: 25min interval ends @23:45 today, begins @00:25 next day).

like image 941
cr8ivecodesmith Avatar asked Nov 18 '11 11:11

cr8ivecodesmith


People also ask

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 you run a cron job every 30 seconds?

But still, you can do some tricky configurations to run your script to run at every 30 seconds. In the above configuration, we have scheduled the script twice. The first cron runs every 1 minute and the second cron also starts at the same time but waits for 30 seconds before execution.


2 Answers

It always splits the current hour only.

40/40 = 1 so it runs every 40th minute of an hour.

*/5 would do 5, 10, 15...

You should go for larger intervals.

Do */30 for your 25 minute interval and every 60 minutes for your 40 minutes interval.

Otherwise set up two crontabs for your script:

0,40 */2 * * * /path/to/script/foo.sh
20 1,3,5,7,9,11,13,15,17,19,21,23 * * * /path/to/script/foo.sh
like image 136
Udo Held Avatar answered Sep 24 '22 12:09

Udo Held


For the task you want to accomplish you have to write a little bit more complex entry in your crontab.

You see the pattern above?

00:40, 01:20, 02:00, 02:40, 03:20 and again 04:00, 04:40, 05:20, 06:00, 06:40, 07:20, 08:00

I can break it down into three entries:

  1. Every even hour you have to run it at 40th min
  2. Every odd hour you have to run it at 20th min
  3. Every even hour you have to run it on 0. (Except 0 hour)

You can accomplish this with more than one entries:

#1
*/40 0,*/2 * * * /path/to/script/foo.sh
#2
*/20 1,*/2 * * * /path/to/script/foo.sh
#3
0 2,*/2 * * * /path/to/script/foo.sh

NOTE: It might have minor issues, but there I gave you direction :)

PS: This will explain alot more

like image 25
DivinesLight Avatar answered Sep 24 '22 12:09

DivinesLight