LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   How would I set a cron job to run every 7 minutes between 7pm and 11pm? (https://www.linuxquestions.org/questions/linux-server-73/how-would-i-set-a-cron-job-to-run-every-7-minutes-between-7pm-and-11pm-849986/)

ghurty 12-13-2010 03:05 PM

How would I set a cron job to run every 7 minutes between 7pm and 11pm?
 
Hi,

How would I set a cron job to run every 7 minutes between 7pm and 11pm?


Thanks

colucix 12-13-2010 03:14 PM

Hi and welcome to LinuxQuestions!
Code:

*/7 19-23 * * * /path/to/job.sh
this will run at 19:00, 19:07, 19:14, 19:21, 19:28, 19:35, 19:42, 19:49, 19:56, 20:00, 20:07 and so on. The count of 7 minutes will restart at minute 00 of every hour, because 7 is not an exact divisor of 60. This bring to an interval of 4 minutes between minute 56 and minute 00 of the next hour. Is this ok? Otherwise you need a slightly more complicate solution.

tronayne 12-13-2010 03:23 PM

OK, 7 PM is 19 Hours, 11 PM is 23 Hours.

A quick check of man crontab indicates that
Code:

# run every seven minutes between 7 pm and 11 pm
0,7,14,21,28,35,42,49,56 19-23 * * * command

Ya can't run every seven minutes 'case 60 is not equally divisible by 7, eh? So we're running at the top of the hour and every seven minutes thereafter except the gap between 56 and 0 (of the next hour).

Hope this helps some.

[EDIT]
Or, the "/7" method indicated above -- duh! forgot about that one...
[/EDIT]

[EDIT]
Duh! As colucix points out below, the hours need to be 19-22, not 23. Arrgghh!
[/EDIT]

colucix 12-13-2010 03:45 PM

To run exactly every 7 minutes in the specified interval we can either add the following condition at the beginning of the script:
Code:

#!/bin/bash
[[ $(( ($(date -d "$(date +%H:%M)" +%s) - $(date -d 19:00 +%s)) % 420 )) -ne 0 ]] && exit

or put it in the crontab entry itself:
Code:

* 19-23 * * * [[ $(( ($(date -d "$(date +\%H:\%M)" +\%s) - $(date -d 19:00 +\%s)) \% 420 )) -eq 0 ]] && /path/to/job.sh
Note that the % signs are escaped in the crontab entry, since they have a special meaning in crontab (please, see man 5 crontab for details). Moreover note that in both cases the job must run every minute.

colucix 12-13-2010 04:06 PM

Oops... just realized that in all the suggested crontab entries, the hour interval must be
Code:

19-22
if you want to stop running at 23:00, otherwise it will continue to run until 23:59. Sorry.


All times are GMT -5. The time now is 07:23 AM.