1752598 Members
5739 Online
108788 Solutions
New Discussion юеВ

Re: Cron help

 
balaji_vvv
Frequent Advisor

Cron help

I have script that needs to run every other Friday? Anyone have running this in cron.

The script needs to run 2/5/10 and next run should be 2/19/10

Thanks for any help.
15 REPLIES 15
Larry Klasmier
Honored Contributor

Re: Cron help

Why not run it every Friday and have the script decide if it should run your job or not. You can accomplish this with a simple if statement. So on 2/5 when the job runs you touch a file. on 2/12 when the job runs it looks for the file. If it finds the file it removes the file and exits. Tne on 2/19 since the file does not exist it will run the job and touch the file.
Michael Steele_2
Honored Contributor

Re: Cron help

Hi

You want to use the modulus operator and check the remainder when divided by 2. There are only two results, 0 or 1. The SUMS stored are integers increasing by one every week. So the operation is 1/2, 2/2, 3/2, 4/2, 5/2, etc.

This becomes a boolean flag for you to trap on. Only trick is to save the last SUM in an file, which becomes an important part of the algorithm.

Here is Midnight every other Friday

weekday
The day of the week, 0-6, 0=Sunday

minute hour monthday month weekday FLAG command

00 00 * * 5 /dir/script

####################
script
#####################

/home/your_dir/FLAG filename.
FLAG=$((cat FLAG_FILE))
echo $FLAG
1

mymodulus=$(( $FLAG % 2 ))
1=1%2

case $FLAG in
"0") Execute script;;
"1") ;;

FLAG=$(($FLAG+1))
echo $FLAG
2
echo $FLAG>FLAG_FILE
##################################
Next time
##################################

/home/your_dir/FLAG filename.
FLAG=$((cat FLAG_FILE))
echo $FLAG
2

mymodulus=$(( $FLAG % 2 ))
0=2%2

case $FLAG in
"0") Execute script;;
"1") ;;

FLAG=$(($FLAG+1))
echo $FLAG
3
echo $FLAG>FLAG_FILE
##################################
Next time
##################################

/home/your_dir/FLAG filename.
FLAG=$((cat FLAG_FILE))
echo $FLAG
3

mymodulus=$(( $FLAG % 2 ))
1=3%2

case $FLAG in
"0") Execute script;;
"1") ;;

FLAG=$(($FLAG+1))
echo $FLAG
4
echo $FLAG>FLAG_FILE

Etc.
Support Fatherhood - Stop Family Law
Steven Schweda
Honored Contributor

Re: Cron help

> [...] If it finds the file it removes the
> file and exits. [...]

Of course, if someone deletes the file, then
you can get the wrong result with no way to
detect the problem. You can also do things
like store a number (say, 0 or 1) in a file,
and look at that value instead of simply
testing the existence of the file.

bash$ echo 0 > flag_file

bash$ var=` cat flag_file `
bash$ var=` expr 1 - $var `
bash$ echo $var
1
bash$ echo $var > flag_file

bash$ var=` cat flag_file `
bash$ var=` expr 1 - $var `
bash$ echo $var
0
bash$ echo $var > flag_file

bash$ var=` cat flag_file `
bash$ var=` expr 1 - $var `
bash$ echo $var
1
bash$ echo $var > flag_file
[...]

Modern shells can do the math without using
"expr", and I didn't show a test for valid
file contents, but, as usual, many things are
possible.
Tingli
Esteemed Contributor

Re: Cron help

You can put a few lines in your script, which can modify file /var/spool/cron/crontabs according to what you need every week. Got to do it carefully, but it wont be that messy.
balaji_vvv
Frequent Advisor

Re: Cron help

Thanks for your ideas. I thought i can directly cron doing the job without any script help.

will this work?
00 08 1-7,15-21 * 3 /home/oracle/test.sh
Steven Schweda
Honored Contributor

Re: Cron help

> will this work?

Do all months have exactly 28 days?
Dennis Handly
Acclaimed Contributor

Re: Cron help

I suppose you could have your script run every Friday and then take the julian day of the year and use that to alternate. Of course do you care that it may do it twice when a new year starts?
if (( $(date +%j) / 7 % 2 == 0)); then
echo Even
else
echo Odd
fi
Muhammad Ahmad
Frequent Advisor

Re: Cron help

Hi balaji,

if you can't handle this in ONE cron job, you can definetly do that by using more than one cron jobs-entries.

is'nt it simple :)

Regards,
Dennis Handly
Acclaimed Contributor

Re: Cron help

>Muhammad: if you can't handle this in ONE cron job, you can definitely do that by using more than one cron jobs-entries.

Oh I see. You fill it up with 25/26 entries and change it each year. :-)