LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 12-13-2010, 03:05 PM   #1
ghurty
LQ Newbie
 
Registered: Dec 2010
Posts: 7

Rep: Reputation: 0
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
 
Old 12-13-2010, 03:14 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 12-13-2010, 03:23 PM   #3
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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 08:11 AM.
 
Old 12-13-2010, 03:45 PM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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 03:49 PM.
 
Old 12-13-2010, 04:06 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to set up a Cron Job to run every other week or every three months etc. kckabobus Linux - Newbie 3 04-03-2008 01:36 PM
Setting up cron to proccess a Bash Script to run every 15 minutes jamtech Programming 3 07-30-2007 03:32 PM
How do I set my profile in CRON job? lolson33 Linux - General 2 12-06-2005 04:18 PM
howto run a script from cron every 5 minutes except 11pm-8am ? cccc Programming 2 03-20-2005 03:52 AM
how to set cron job to run every 5 mins? gsbarry Linux - General 7 05-28-2003 12:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 02:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration