cancel
Showing results for 
Search instead for 
Did you mean: 

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
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. :-)
Patrick Wallek
Honored Contributor

Re: Cron help

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

No, I don't think this will do what you want. This job will run on the 1-7, the 15-21 AND on Friday. The job will run IF ANY of the last 3 fields are true.

Note the example from the cron man page:


Note that the specification of days can be made in two fields: monthday and weekday. If both are specified in an entry, they are cumulative. For example,

0 0 1,15 * 1 command

runs command at midnight on the first and fifteenth of each month, as well as every Monday.


>>Do all months have exactly 28 days?
Well, yes they do! ;)
Steven Schweda
Honored Contributor

Re: Cron help

> >>Do all months have exactly 28 days?
> Well, yes they do! ;)

This must be some new meaning for the word
"exactly" (which I used for a reason).
balaji_vvv
Frequent Advisor

Re: Cron help

I finally ended up using Larry Klasmier idea.

Thanks for everyone contributed.
Here is the logic, if anyone needs it.
This will run biweekly sunday at 10.00am.

My cron
--------
00 10 * * 6 /myscript

My script
---------

#! /usr/bin/bash

if [ -e /tmp/fchek ] ; then
rm -f /tmp/fchek
else
touch /tmp/fchek
exit 0
fi

sh /myscript

exit 0

Explanation : (Not for experts!)
-------------
Script will look for a file /tmp/fcheck on the first run, it wont find it and it will create and exit. On the next week it will find the file, remove it and execute my script. On third week it wont find my file, create it and exit and so on....
balaji_vvv
Frequent Advisor

Re: Cron help

Resolved. See above
Steven Schweda
Honored Contributor

Re: Cron help

> rm -f /tmp/fchek
> else

If someone changes the permissions on this
file, then the "rm" could fail (silently).

Of course, this should never happen. How
important is it that this job run exactly
every two weeks? Sometimes the easy way is
good enough. Sometimes it's not. You get to
decide what's good enough.
balaji_vvv
Frequent Advisor

Re: Cron help

I will keep this file in secure location. It's not very critical if it fail to run, as everyone in that group will get email, and they will take care of it.