Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-13-2010, 04:05 PM
|
#1
|
LQ Newbie
Registered: Dec 2010
Posts: 7
Rep:
|
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
|
|
|
12-13-2010, 04:14 PM
|
#2
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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.
|
|
|
12-13-2010, 04:23 PM
|
#3
|
Senior Member
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
|
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]
Last edited by tronayne; 12-14-2010 at 09:11 AM.
|
|
|
12-13-2010, 04:45 PM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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.
Last edited by colucix; 12-13-2010 at 04:49 PM.
|
|
|
12-13-2010, 05:06 PM
|
#5
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
Oops... just realized that in all the suggested crontab entries, the hour interval must be
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 02:57 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|