LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-27-2009, 01:21 PM   #1
ITH3
LQ Newbie
 
Registered: Oct 2003
Posts: 3

Rep: Reputation: 0
cronjob every 30 hours?


I have a pretty good understanding how the crontab is formatted but have no idea how to write the cronjob entry to execute a script every 30 hours, is it even possible? Thanks.
 
Old 11-27-2009, 01:37 PM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

I might be very wrong but I doubt that it's possible using cron. The range for the hour statement is 0-23 as far as I know. What you might do is write your own script and use the at command, don't know if that will work but if one can say:
Code:
at now +4 days 
run_script
then it would be logical that you could say
Code:
at now +30 hours
since at works with a sort of timestamp when the at command was timed and calculates from there, where cron references to a time within a period (does this makes sence?).

If you could write your script with the at command (haven't tried it), then you could easily make it 'rewrite' itself every 30 hours when it ran.

Kind regards,

Eric
 
Old 11-27-2009, 04:33 PM   #3
Tuxqi
Member
 
Registered: Apr 2009
Posts: 49

Rep: Reputation: 2
I think using a combination of the sleep command in a script is the answer.
 
Old 11-27-2009, 05:49 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
Using at and submitting a new job from inside the script itself is a good idea. There is just a downside to take in account: if the machine is switched off for some reason at the time the job is scheduled, the at daemon will run the "expired" job at the successive boot up (this issue is discussed in a recent thread, here). If this happens the regular 30-hours scheduling is delayed and maybe this is an unwanted effect.

Another solution can be the following:
Code:
#!/bin/bash
REFTIME=1259449200
if [ $(date +%s) -ge $REFTIME ]
then
  <your code here>
  <...>
  sed -i "/REFTIME=/s/$REFTIME/$((REFTIME+108000))/" $(readlink -f $0)
fi
This script records a reference time in seconds since epoch, which is the time at which the job is scheduled. The if/then construct checks if the current time is greater than or equal to the reference time and if the condition is matched it executes the code AND edits itself to update the reference time by 30 hours (it adds 108000 seconds to the reference time).

In this way you can run this job every 10 hours, using a crontab entry like
Code:
0 */10 * * * /path/to/the/script
and the block of code inside the if/then will be executed alternatively 1 time every 3. That is every 30 hours. Choose carefully the first reference time, that will be the actual time at which the cron job is executed for the first time, and the trick is done!

Edit: just thinking about the crontab entry... maybe even better is to run the script at fixed hours, so you can establish the starting reference time more precisely. Suppose you run the first job at midnight. Then adding 30 hours make the job to run at 6 of the next day, then at 12 of the day after and so on. Hence an entry like this
Code:
0 0,6,12,18 * * * /path/to/script
should be enough and you can easily determine the starting reftime using for example
Code:
date -d "20091129 00:00:00" +%s

Last edited by colucix; 11-27-2009 at 06:24 PM. Reason: Suggested alternative crontab entry
 
Old 11-28-2009, 02:25 AM   #5
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hi,

Thanks colucix for pointing out the downside, that's something I didn't take into account.

Kind regards,

Eric
 
Old 11-28-2009, 02:40 AM   #6
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 Eric,

unfortunately re-reading this thread after your reply... I realized the same downside is in the crontab method suggested: if the computer is down at the scheduled time, the script is not updated. I have to rethink about it. Thanks.

Kind regards,
Alex
 
Old 11-28-2009, 02:45 AM   #7
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello Alex,

Wouldn't it be possible to write output to a file when the script was executed successfully and then check the outputfile for the last runtime? If the outputfile isn't present or out of date then it's safe to assume that the script was not executed, and then a procedure has to be launched to reset the reference time. Of course the first time the outputfile has to be generated by hand since it will be checked when first run.

Just thinking out wild, but that might solve the problem no?

Kind regards,

Eric
 
Old 11-28-2009, 03:36 AM   #8
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
Yes, it might solve the problem. You have just to store the file somewhere and (over)write the date into it every time the script is executed (or better when the block of code inside an if/then construct is executed).

Here is an alternative, which simply checks the current date without reference time (except for unix time) and without external files:
Code:
#!/bin/bash
time=$(($(date +%s)/3600))
if awk -v time=$time 'BEGIN{exit time%30}'
then
  <your code here>
  <...>
fi
The computed time at the beginning of the script is the integer of the number of seconds since epoch expressed in hours. Here we take advantage of the fact that bash does not manage floating point numbers, hence the result of the division is a truncated integer. The integer is necessary to take into account the 1 or more seconds delay in the execution of the script (in respect of the exact scheduled time).

Then it simply checks if the number of hours since epoch is exactly a multiple of 30. The only and mandatory constrain is that you have to start the job at a time (since epoch) which is already a multiple of 30 hours.

The next (based on CET time zone, not UTC) useful start times are:
Code:
Sun Nov 29 13:00:00 CET 2009
Mon Nov 30 19:00:00 CET 2009
Thu Dec  3 07:00:00 CET 2009
if the machine uses UTC the starting point have to be recalculated.

In my example the crontab entry would be something like:
Code:
0 1,7,13,19 * * * /path/to/script
To avoid the aforementioned constrain, we can try to simply add (or substract) an offset from the computed time. Once the offset is carefully determined, we can start our cron job at every hour and minute, at our pleasure.

Last edited by colucix; 11-28-2009 at 03:40 AM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux machine gets stuck after 8 hours or 12 hours fahadaziz Linux - Newbie 4 03-28-2010 04:19 PM
how do i get a cronjob to run every two hours in AIX: 00 */2 * * * command no work boyd98 AIX 1 10-19-2009 06:33 PM
cronjob xser Linux - Software 6 07-27-2008 09:38 PM
cronjob ugob Linux - Software 0 03-03-2004 09:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:09 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