LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 05-07-2010, 08:18 AM   #1
Norwood
Member
 
Registered: Feb 2010
Location: A Smidge South O' Boston
Distribution: Debian
Posts: 41

Rep: Reputation: 15
Cron job that needs to run at kind of an odd interval


So I know that if I need something to run every 15 minutes I would use:

Code:
0,15,30,45 * * * * /my/command
However, this particular job needs to run every 1 hour and 48 minutes.

Would I use:

Code:
0,108,216,324 * * * * /my/command
If not what can I do to accomplish this?

To be extra clear, it needs to be 1.8 hours after each job completes. So it couldn't be something like:

Code:
10 * * 1 * /my/command
Which means every 10 minutes after the hour. Because the minutes hand will be different and the hour will change...am I making sense?

Last edited by Norwood; 05-07-2010 at 08:24 AM.
 
Old 05-07-2010, 08:34 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Out of curiousity, I just stared at the man page for as long as I could stand it.....It **implies** that the fields are what you would expect with something like the date command---ie the minutes field only goes to 59 or 60.

I don't see a solution other than trying it. Set up a simple test and let it run overnight.

I would first try:
*/108 * * * *
 
Old 05-07-2010, 12:21 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,366

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
I doubt an interval in minutes that is not a divisor of 60 works as expected. Anyway, it is worth to try and find out.

Another solution is a bit more tricky and it requires a check at the beginning of the script. First we consider at which time the script should run: suppose it starts at midnight of a certain day, then by adding 1h and 48m progressively we get:
Code:
 0:00   1:48   3:36   5:24   7:12   9:00  10:48  12:36  14:24  16:12
18:00  19:48  21:36  23:24   1:12   3:00   4:48   6:36   8:24  10:12
12:00  13:48  15:36  17:24  19:12  21:00  22:48   0:36   2:24   4:12
 6:00   7:48   9:36  11:24  13:12  15:00  16:48  18:36  20:24  22:12
 0:00
that is after three days the script would run at midnight again. Furthermore, as you can notice, the script would run always at minutes 0, 12, 24, 36, 48 so that the crontab entry can be
Code:
0,12,24,36,48 * * * * /my/command
Now in the script we have to be sure that the code will be executed only at 0:00, 1:48, 3:36, ... of the first day, at 1:12, 3:00, 4:48, ... of the second day and at 0:36, 2:24, 4:12, ... of the third day. And so on for every three-days period.

To do this we have to define a reference date as the date of the first day at midnight at which we want to start our count of three-days periods. Let it be for example midnight of 8-May-2010. All the calculations are processed in seconds and... here we go:
Code:
#!/bin/bash
#
#  Set the reference time
#
reftime=$(date -u -d 20100508 +%s)
#
#  Compute the current time in seconds since epoch.
#
#   The inner date command is to take in account the lag of 1
#   or more seconds between the time specified in crontab and
#   the actual time of execution. In practice remove seconds
#   from the current time.
#
date=$(date -u -d "$(date -u +"%Y%m%d %H:%M")" +%s)
#
#  Retrieve hour and minutes in HHMM format
#
hm=$(date -u -d @$date +%H%M)
#
#  Initialize a counter. This will be the number of intervals
#   of 108 minutes, that represents how long we are distant
#   from 00:00 of the first day of the current three-days period. 
#
count=0
#
#  Now subtract 108 minutes from the current date progressively, 
#   until we reach the previous midnight. Do this for a maximum
#   of 40 times (if not it might generate an infinite loop when
#   we are at a certain time of the day)
#
while [ $hm -ne 0 -a $count -lt 40 ]
do
  ((count++))
  hm=$(date -u -d @$(($date - 6480 * $count)) +%H%M)
done
#
#  Compute the date in seconds since epoch at which it reached
#   midnight or it went back in time for 3 days (time is not
#   midnight but the number of 108-minutes intervals is 40)
#
date=$(date -u -d @$(($date - 6480 * $count)) +%s)
#
#  Check now! Compute the difference between the resulting
#   date and the reference time, divide by 3 days (in seconds)
#   and if the remainder is 0 execute the code, otherwise do
#   nothing
#
if [ $(( ($date - $reftime) % 259200 )) -eq 0 ]
then
  < code here >
fi
In practice, it checks that the distance (in time) between the date computed by subtracting 108 minutes progressively and the reference time is exactly a multiple of 3.

Note that all the dates computed by the date command are in UTC time. This should avoid problems in calculations at the passage from and to Daylight Saving Time. Anyway, I'm not sure if this passage will generate some other problems (further testing needed). Hope this helps.
 
Old 05-09-2010, 10:18 AM   #4
Norwood
Member
 
Registered: Feb 2010
Location: A Smidge South O' Boston
Distribution: Debian
Posts: 41

Original Poster
Rep: Reputation: 15
Very nice solution fellas.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run cron job in every 20 second ust Linux - Newbie 12 09-22-2008 11:04 PM
Can't get a cron job to run derzok Linux - General 10 12-16-2007 04:00 AM
how to run this cron job ashley75 Linux - General 5 05-24-2004 11:20 AM
Did my Cron job run? ryedunn Linux - Newbie 2 02-25-2004 08:59 AM
Cron job does not run brentos Linux - General 6 12-12-2003 02:37 PM


All times are GMT -5. The time now is 04:29 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration