Operating System - HP-UX
1837967 Members
2833 Online
110124 Solutions
New Discussion

Re: How to schedule a job every other day?

 
SOLVED
Go to solution
David Yandry
Frequent Advisor

How to schedule a job every other day?

Hi Experts,

This should be a very simple task. I need to schedule a file transfer every other day at 5:00 AM. I can't find a combination of cron options that work. Any ideas?

Thanks,
David
3 REPLIES 3
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to schedule a job every other day?

The solution is rather straightforward. You need to simply run your script every weekday at 0500 from cron. The script itself will then determine if it needs to do the transfer or simply exit.

One naive approach would be
if [[ $(($(date) '+%j' % 2)) -eq 0 ]]
then
exit 0
fi

BUT don't do it because I've been told that some years have 365 days and others have 366 - your calculations will go awry.

Approach 2) Use an at command within your script to reschedule in 48 hours but don't do that neither. A chain is only as strong as its weakest link If the command ever fails before the at is issued, all subsequent commands fail.

Approach 3) Works everytime.
if [[ $(($(caljd.sh) % 2)) -eq 0 ]]
then
exit 0
fi

You may need to change the -eq 0 to -eq 1 but you get the idea. Search the Forums for a copy of caljd.sh. Version 2.1 is the latest. Caljd.sh -u will give full usage.

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to schedule a job every other day?

Hi David:

Approach 4) Works everytime as well:

if [[ $(perl -e 'use integer; print ((scalar time / 86400) % 2),"\n"') -eq 0 ]]
then
exit 0
fi

This simply divides epoch seconds by 86400 (number of seconds per day) to get days and then does a mod 2.
If it ain't broke, I can fix that.
curt larson_1
Honored Contributor

Re: How to schedule a job every other day?

approach 5) you could write your own C program to get the number of seconds from the epoch using the ctime function. But, your system probably has perl already, and not an ansi C compiler.

approach 6) use a temp file to remember what was done yesterday, ie. day 1, temp file doesn't exist so create it, day 2, temp file exists so send file and remove temp file. But, it does have problems if the script is run multiple times on one day or if a day is missed.