Operating System - HP-UX
1826428 Members
3260 Online
109692 Solutions
New Discussion

Need to execute a task every 10 days

 
SOLVED
Go to solution
Greg White
Frequent Advisor

Need to execute a task every 10 days

Hi experts,

I have a requirement to execute a task at the same time every 10 days. I can't get cron to do this. Any ideas?

TIA,
Greg
I know how to do it in pascal.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor
Solution

Re: Need to execute a task every 10 days

Hi Greg:

Launch your script. At its epilog, have the script schedule itself to run again in 10-days time using 'at'.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Need to execute a task every 10 days

You are probably going to be told to use at to launch your script and put a command something like this "at -f mycommand now + 10 days" at the end of your script so that the current job schedules its successor. I don't like that approach because if the chain ever breaks, you command never recovers.

I would let the script run every day at the appointed time and let it decide if it is the 10th day. If so, execute else exit immediately. You should have no trouble finding a copy of caljd.sh. I'll assume that you have installed it in /usr/local/bin.

--------------------------------
#!/usr/bin/sh

export PATH=${PATH}:/usr/local/bin

typeset -i DAY=0
typeset -i DO_DAY=5 # set between 0-9
typeset -i STAT=0

DAY=$(($(caljd.sh) % 10))
if [[ ${DAY} -eq ${DO_DAY} ]]
then
echo "Do your thing"
STAT=${?}
fi
exit ${STAT}

--------------------------------------
set DO_DAY to a value between 0 and 9 because I don't know on which day you wish to start.

By the way, DON'T try to use date '+%j' because year boundaries are going to kill you. Caljd.sh will return an integer for each day indicating days since 4713BCE.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Need to execute a task every 10 days

Yet another way to get sequential days is Perl:

DAY=$(perl -Minteger -e 'print ((time() / 86400) % 10)'
if [[ ${DAY} -eq ${DO_DAY} ]]
then

fi
If it ain't broke, I can fix that.
Greg White
Frequent Advisor

Re: Need to execute a task every 10 days

Thanks for the quick replies.

Wow, you guys are quick. I think I will use A. Clay's approach because as he pointed out if my task ever fails, it won't automatically restart. I am concerned that running the task everyday might put a load on my machine so the at approach is better from that perspective.

Thanks,
Greg

I know how to do it in pascal.
A. Clay Stephenson
Acclaimed Contributor

Re: Need to execute a task every 10 days

If your box is so loaded that a cron job fired once per day is significant then you have much bigger fish to fry. Really, the additional loading is trivial and I would trade reliability and recovery for the trivial additional overhead of 9 useless cron jobs every 10 days every time. With a bit more logic, you could have each "real" job write a Julian Day to a file recording the last time the script actually ran. You could then test to see if today is > 10 days from that date and run eventhough today is not actually a 10th day. This would cause the script to run in case the box were down for maintenance or something on the real "10th" day. Invoke as caljd.sh -u for full usage and look for Version 2.3 s. It's the latest and greatest.
If it ain't broke, I can fix that.