setup cron tab to specific time of during weekdays

LinuxCron

Linux Problem Overview


I am trying to setup a cron job on a Ubuntu server. We want the cron job to run the script at certain times of the day and on some specific days of the week. For example, we want to setup a cron job that runs the script with the following sequence:

> Execute the script every 2 minutes from 9 am to 2 pm during the weekdays.

This is what I have been able to do so far:

> */2 09-14 * * * /path_to_script

What should I do for the weekdays?

Linux Solutions


Solution 1 - Linux

Same as you did for hours:

*/2 09-18 * * 1-5 /path_to_script

0 and 7 stand for Sunday
6 stands for Saturday
so, 1-5 means from Monday to Friday

Solution 2 - Linux

You state 2pm in your requirement, hour range should end at 14 instead of 18 (which is 6pm).

*/2 9-14 * * 1-5 /path_to_script

man crontab

http://unixhelp.ed.ac.uk/CGI/man-cgi?crontab+5

Solution 3 - Linux

In fact the last hour you want the script to run is 13:00 to 13:59, so you want:

*/2 9-13 * * 1-5 /path_to_script

meaning the first runtime will be 9:00, then 9:02, and so on until 13:58 which will be the last run as 14:00 is not included.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNauman BashirView Question on Stackoverflow
Solution 1 - LinuxThanksForAllTheFishView Answer on Stackoverflow
Solution 2 - LinuxEJWView Answer on Stackoverflow
Solution 3 - LinuxFonantView Answer on Stackoverflow