1833016 Members
2170 Online
110048 Solutions
New Discussion

scheduling cron job

 
Paul Wright
Advisor

scheduling cron job

Is it possible to schedule a cron job to run every OTHER Saturday? I can't set it up to use 2 days, like the 1st & 15th, due to circumstances that are too lengthy to explain.
Thanx in advance for any help.
10 REPLIES 10
Sachin Patel
Honored Contributor

Re: scheduling cron job

He paul,
You can do this in your script. If you running any program then call that prgram from script.
Now run your program from cron for all sat. and it will run only on even sat.


set WEEK = `date +%U`
@ ALTWK = $WEEK % 2
switch ($ALTWK)
case 0:
set SET = 1
breaksw
case 1:
set SET = 2
breaksw
endsw
if ( $SET == 1 )
call program;
else
exit 0;
endif
Is photography a hobby or another way to spend $
Abel Berger
Regular Advisor

Re: scheduling cron job

Hi Paul,
If I understand you , you want not that the script run in 1st and 15th saturday of the year, correct ??
If I correct, you will can to note every
saturdays of the year and put it in the cron
line. For exmple this month ( July ) have a
four saturdays ( 7 14 21 28 ) then you crontab line will stay, for example, as below :

0 4 14,21,28,.....* * /dir/comand

The crontab will execute the more restrictive comand, in this case, it ignored the * * and
run only in the days that was specify.

I hope this help.

Regards,

Abel Berger

John Poff
Honored Contributor

Re: scheduling cron job

Hello,

Another way to do it is to put it into a script that uses 'at' to reschedule itself each time it runs.

You could have a script named 'myscript' that looks like this:

#!/bin/sh

# myscript

# Start of script
.
.
# End of script

echo "sh /home/users/myhome/myscript" | at 1900 saturday next +3 weeks


The next parameter in 'at' defaults to 'next +1', so to get it set two weeks out you would do 'next +3'.

Just another way to do it.

JP

Jack Werner
Frequent Advisor

Re: scheduling cron job

Schedule it to run on day "6" every week. You will get into a problem running every other week in a month. Some month's have 5 Saturdays. If you simply want to run every-other week regardless of the month, then test the existence of a file and exit if present.(eg $HOME/.BYPASS), otherwise, touch $HOME/.BYPASS and run the script.
i'm retired
Jack Werner
Frequent Advisor

Re: scheduling cron job

My previous solution is in error.

if .RUN then
process
mv .RUN .SKIP
exit
else
mv .SKIP .RUN
fi
exit
i'm retired
A. Clay Stephenson
Acclaimed Contributor

Re: scheduling cron job

Hi Paul,

I think I have the perfect way to do this and it uses a technique that I have used for many years. Use the attached script to return a Julian Day (~ No. of days since 01/01/4712 BCE - Astronomers use a variant of this to make calculations simple).
You then divide this by 7 to get get the week number. Mod this by 2 and you get either 1 or 0. The advantage of this, is that it truly does work every other week. The %U format operator in date sometimes does not work as expected because some years have 53 weeks and most have 52.
Your cron entry should of course fire off each Saturday and then call a script something like this: (caljd.sh is the attached script)

#!/usr/bin/sh
JDAY=`/usr/local/bin/caljd.sh`
JWK=$((${JDAY} / 7))
IS_WK=$((JWK} % 2))
if [ ${IS_WK} -eq 1 ]
then
echo "Do your Stuff Goes here"
else
echo "Do nothing"
fi

This will absolutely work, year in, year out.

Depending on your application, you might actually fire off your application on 0 rather than 1 but you get the idea.

Regards, Clay
If it ain't broke, I can fix that.
John Henrikson
Regular Advisor

Re: scheduling cron job

A. Clay -
I've been using your method for months to automate a job I have to run on alternate tuesdays.. Today it failed.
It ran as usual on 1/7 and 1/21 but didn't fire off the job today 2/4/03.. ?
Any ideas why?
thanks so much!

John Henrikson

A. Clay Stephenson
Acclaimed Contributor

Re: scheduling cron job

Because you are running a very old version of caljd.sh that had a bug. I missed a floating-point divide (rather that integer).

Use this version and all should be well. In the comments sectiopn, I indicate my dumb error.


If it ain't broke, I can fix that.
John Henrikson
Regular Advisor

Re: scheduling cron job

Thanks so much!!!

John Henrikson
David_246
Trusted Contributor

Re: scheduling cron job

Hi Paul,

0 0 * * * 6 /usr/local/bin/run_every_two_weeks

---------------
#!/bin/ksh
## Run every_two_weeks_script

# Get current weeknumber
week=`cat /usr/local/etc/week_number`

# Only for the first time when $week is not yet set :
if [ -z $week ]
then
week="0"
fi

if [ $week -eq "1" ]
then
/run/this/script
echo "0" >/usr/local/etc/week_number
else
echo "1" >/usr/local/etc/week_number
fi
# ---------------------------


Regs David
@yourservice