Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running Cron Job at different frequencies throughout day

Tags:

cron

crontab

Is it possible to run a cron job for different frequencies throughout the day? Or to achieve the same effect, is it possible to have the cron job run on a regular frequency but only during certain hours of the day?

Example I would like to run my script 6/hour between 11am - 2pm, and 6/hour between 5pm - 8pm. Otherwise, I would like the script to run 1/hour.

like image 537
user3608238 Avatar asked Aug 11 '14 14:08

user3608238


People also ask

How do I run cron in alternate days?

1 Answer. The cron statement for each script execute on the same days - */2 evaluates to 1-31/2 . See my answer here for more details. To get alternating days, you could use 2-31/2 for the first script - this would start at 2 and skip every other for 2,4,6 etc.

What does 0 * * * * mean in crontab?

0 * * * * -this means the cron will run always when the minutes are 0 (so hourly) 0 1 * * * - this means the cron will run always at 1 o'clock. * 1 * * * - this means the cron will run each minute when the hour is 1.

Does crontab run sequentially?

Yes, using ; between the commands will guarantee that all commands are run sequentially, one after the other. The execution of one command would not depend on the exit status of the previous one.


1 Answers

You can for example use:

*/10 11-13,17-19   * * * /your/script  # every 10 min 11.00 to 13.00, 17.00 to 19.00
0 0-10,14-16,20-23 * * * /your/script  # every 1 hour 00.00 to 10.00, 14.00 to 16.00, 20.00 to 23.00
  • */10 11-13,17-19 * * * means: every 10 minutes on the hours 11 to 13 and 17 to 19. That is, to happen between, 11.00 and 13.59 and 17.00 and 19.59.
  • 0 0-10,14-16,20-23 * * * means: every minute 0 on the hours 0 to 10, 14 to 16 and 20 to 23. That is, to happen at exactly hours 0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 14, 15, 16, 20, 21, 22 and 23 (all but 11, 12, 13, 17, 18 and 19).

Remember the format is like this:

 +---------------- minute (0 - 59)
 |  +------------- hour (0 - 23)
 |  |  +---------- day of month (1 - 31)
 |  |  |  +------- month (1 - 12)
 |  |  |  |  +---- day of week (0 - 6) (Sunday=0 or 7)
 |  |  |  |  |
 *  *  *  *  *  command to be executed
like image 191
fedorqui 'SO stop harming' Avatar answered Sep 23 '22 19:09

fedorqui 'SO stop harming'