I want to start a script with a cronjob every fourth sunday or the first
sunday in the month.
Is there a possibilty to realize this?
Thanks
Dieter
Good method is to make a cronjob run a front end script on sundays,
The front end script will determine if it's currently the first
or fourth sunday. If it is, then execute the program desired.
- Matt
--
_______________________________________________________________________
Matthew Landt - AIX and HACMP Cert. Specialist - la...@austin.ibm.com
<< Comments, views, and opinions are mine alone, not IBM's. >>
> I want to start a script with a cronjob every fourth sunday or the first
> sunday in the month.
as the manual says (under FreeBSD) :
Commands are executed by cron(8) when the minute, hour,
and month of year fields match the current time, and when at least one of
^^^^^^^^^^^^^^^
the two day fields (day of month, or day of week) match the current time
^^^^^^^^^^^^^^^^^^
try something like (untested) :
so, it seems they can be combined, such as (untested) :
0 0 24-31 * 0 for the last sunday
0 0 1-7 * 0 for the first sunday
Cyrille.
--
home: mailto:clefevre%no-...@poboxes.com.invalid | UNIX is user-friendly;
work: mailto:Cyrille.Lefevre%no-...@edf.fr.invalid | it's just particular
Supprimer "%no-spam" et ".invalid" pour me repondre. | about who it chooses
Remove "%no-spam" and ".invalid" to answer me back. | to be friends with.
You got it backwards. **at least one of the two** means "or". Your scripts
will run
(a) every Sunday and the 24th through the 31st
(b) the 1st through the 7th and every Sunday
hymie! http://www.smart.net/~hymowitz hy...@lactose.smart.net
===============================================================================
It was a slap in the face how quickly I was replaced
And are you thinking of me when you ... --Alanis Morissette
===============================================================================
> Mon, 21 May 2001 13:59:33 -0500 Matthew Landt <la...@austin.ibm.com> wrote:
> | Dieter Kroemer wrote:
> |>
> |> I want to start a script with a cronjob every fourth sunday or the first
> |> sunday in the month.
> |>
> |> Is there a possibilty to realize this?
> |>
> | Good method is to make a cronjob run a front end script on sundays,
> | The front end script will determine if it's currently the first
> | or fourth sunday. If it is, then execute the program desired.
>
> I have often wanted something like this but have not been able to
> figure it out. If you get a script that can tell which Sunday it
> is and then run the command, please share it.
if [ $(( (`date +%d` - 1) / 7 + 1)) -eq 4 ]
then
run command
fi
--
Chris F.A. Johnson bq...@torfree.net
=================================================================
c.f.a....@home.com http://cfaj.freeshell.org
cf...@freeshell.org http://members.home.net/c.f.a.johnson
>|> I want to start a script with a cronjob every fourth sunday
>|> or the first sunday in the month.
>
> If you get a script that can tell which Sunday it is and then
> run the command, please share it.
Let cron run the script every Sunday, then let the script decide
whether to run the full script or not based on what Sunday it is,
perhaps along the lines of
#v+
case $(date +%d) in
01|02|03|04|05|06|07)
echo First Sunday
;;
08|09|10|11|12|13|14)
echo Second Sunday
;;
15|16|17|18|19|20|21)
echo Third Sunday
;;
22|23|24|25|26|27|28)
echo Fourth Sunday
;;
29|30|31)
echo Fifth Sunday
;;
*)
echo Rats, an error...
exit 1
;;
esac
#v-
// Klaus
--
><>°
[snip]
> You got it backwards. **at least one of the two** means "or". Your scripts
> will run
ah! ok. didn't understand that. thanks.
Cyrille.
--
home: mailto:clefevre%no-...@redirect.to.invalid | UNIX is user-friendly;
I want to start a script with a cronjob every fourth sunday
or the first sunday in the month.
If you get a script that can tell which Sunday it is and then
run the command, please share it.
[/question]
Let cron run the script every Sunday, then let the script decide whether to
run the full script or not based on what Sunday it is, perhaps along the
lines of
#v+
case $(date +%d) in
01|02|03|04|05|06|07)
echo First Sunday
;;
08|09|10|11|12|13|14)
echo Second Sunday
;;
15|16|17|18|19|20|21)
echo Third Sunday ;;
22|23|24|25|26|27|28)
echo Fourth Sunday ;;
29|30|31)
echo Fifth Sunday ;;
*)
echo Rats, an error...
exit 1 ;;
esac
#v-
// Klaus
--
><>°
I like.....I like!
Dano
You can do this simply piping cal to and awk;
Manipulate $day and/or NR/awk action to get required day/week:
Format of Cal:
May 2001
S M Tu W Th F S
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
day=1 # Sun=1, Mon=2, Tue=3, etc.
# Change value to get differnt day.
for target in $(cal | awk '
NR == 3 {
for (i = 0; i < NF; i++)
$( 7 - i ) = $( NF - i )
for (i = 1; i <= ( 7 - NF ); i++)
$i = ""
}
NR >= 3 { print $x } # Change this line to get different week
# NR >= 3 for the last week of month
# NR == 1 for the 1st week
# NR == 2 for the 2nd week, etc.
' x=$day )
do
: # Loop until no more field.
# Last non-null value would be retained.
done
[[ $(date "+%d") = $target ]] && some_cmd
--
Regards
========= start script ========
NWDAY() {
# params:
# 1 = weekday (1=Sunday ... 7=Saturday)
# 2 = occurence
# 3 = month (for 'cal', optional)
cal $3 |
sed '
# delete lines with alfa chars
/[a-z]/d;
# delete empty lines
/^$/d
# fill first days of month with -
s+ + - +g;
' |
awk '
{
Q=$wd;
if ( Q != "-" ) {
t++;
if ( t == n ) { R=Q; }
}
}
END{ print R; }
' wd=$1 n=$2
}
DAY1=`NWDAY 1 1 "6 2001"`
DAY4=`NWDAY 1 4 "6 2001"`
echo "DAY1=$DAY1 DAY4=$DAY4"
# now check if today's the day
curday=`date "+%d"`
[[ $curday = $DAY1 || $curday = $DAY4 ]] && some_cmd
========= end script ========
Have fun,
-BJ-
Yeow Ling, Tjioe <yltj...@netscape.net> schreef in artikel
<3B131885...@netscape.net>...
> I've come up with the following script :
>
> ========= start script ========
> NWDAY() {
> # params:
> # 1 = weekday (1=Sunday ... 7=Saturday)
> # 2 = occurence
> # 3 = month (for 'cal', optional)
> cal $3 |
> sed '
[snip]
> # now check if today's the day
> curday=`date "+%d"`
> [[ $curday = $DAY1 || $curday = $DAY4 ]] && some_cmd
> ========= end script ========
Incredibly complicated solution for a simple task.
Assuming the script is called from a crontab every Sunday, there is no
need to check what day of the week it is (and if there were, "date +%w"
would do the trick).
day=`date +%d`
week=$(( ($day - 1) / 7 + 1 )) # if your shell doesn't have arithmetic,
# use expr
if [ $week -eq 1 -o $week -eq 4 ]
then
do_whatever
fi
On the other hand if the OP want "every fourth Sunday" rather than the
first and fourth Sundays of each month, get the week of the year with
yrwk=`date +%U` # [ or +%V ] (are these standard? GNU date has them)
week=$(( yrwk % 4 ))
This might need a little tweaking to cover years with 53 Sundays.
"Chris F.A. Johnson" wrote:
>
> Incredibly complicated solution for a simple task.
>
> Assuming the script is called from a crontab every Sunday, there is no
> need to check what day of the week it is (and if there were, "date +%w"
> would do the trick).
>
> day=`date +%d`
> week=$(( ($day - 1) / 7 + 1 )) # if your shell doesn't have arithmetic,
> # use expr
> if [ $week -eq 1 -o $week -eq 4 ]
> then
> do_whatever
> fi
I agree the post before was way too complicated. The math above is
very nice if you can GUARANTEE the day executed is a sunday. If not
you can use the below to GUARANTEE that it is the FOURTH sunday
TODAY (using KSH)... written for clarity.
#Begin
fourthsun=$(cal |awk '{if ($0 ~/^[ 0-9][0-9]/) {count++};
if (count == 4) {print substr ($0,0,2)}}')
today=$(date +%d)
if [[ $fourthsun -eq $today ]]
then
echo "Today is the fourth sunday"
fi
#end
> > Assuming the script is called from a crontab every Sunday, there is no
> > need to check what day of the week it is (and if there were, "date +%w"
> > would do the trick).
> >
> > day=`date +%d`
> > week=$(( ($day - 1) / 7 + 1 )) # if your shell doesn't have arithmetic,
> > # use expr
> > if [ $week -eq 1 -o $week -eq 4 ]
> > then
> > do_whatever
> > fi
>
> I agree the post before was way too complicated. The math above is
> very nice if you can GUARANTEE the day executed is a sunday. If not
if not (as I posted above), use "date +%w"
> you can use the below to GUARANTEE that it is the FOURTH sunday
> TODAY (using KSH)... written for clarity.
>
> #Begin
> fourthsun=$(cal |awk '{if ($0 ~/^[ 0-9][0-9]/) {count++};
> if (count == 4) {print substr ($0,0,2)}}')
> today=$(date +%d)
> if [[ $fourthsun -eq $today ]]
> then
> echo "Today is the fourth sunday"
> fi
> #end
The only external call necessary is date, and only once:
date "+%w %d" | {
read dow day
week=$(( ($day - 1) / 7 + 1 ))
if [ $dow -eq 0 -a $week -eq 1 -o $week -eq 4 ]
then
echo week $week
fi
Similar to what I did before I saw your post:
if [[ $(date '+%d') = $(cal | awk '/^[ 0-9][0-9]/ {i++} i==4 && $0 {print
$1}') ]]; then
echo 4th sunday
else
echo not 4th sunday
fi
Very nice idea, but isn't this a little off... I understand
day of week = 0 to check for sunday. Then why check for week = 1?
Also did you want the logic: [ ($dow -eq 0 -a $week -eq 1) -o $week -eq 4 ]
or did you want: [ $dow -eq 0 -a ($week -eq 1 -o $week -eq 4) ]
Using the origional logic this will return true if: Today is sunday
AND it is the first sunday. It will ALSO return true if today is
any day during the fourth week. I would think that the logic
[ $dow -eq 0 -a $week -eq 4 ] should do the trick.
> "Chris F.A. Johnson" wrote:
> >
> > The only external call necessary is date, and only once:
> >
> > date "+%w %d" | {
> > read dow day
> > week=$(( ($day - 1) / 7 + 1 ))
> > if [ $dow -eq 0 -a $week -eq 1 -o $week -eq 4 ]
> > then
> > echo week $week
> > fi
> > }
> >
>
> Very nice idea, but isn't this a little off... I understand
> day of week = 0 to check for sunday. Then why check for week = 1?
> Also did you want the logic: [ ($dow -eq 0 -a $week -eq 1) -o $week -eq 4 ]
> or did you want: [ $dow -eq 0 -a ($week -eq 1 -o $week -eq 4) ]
>
> Using the origional logic this will return true if: Today is sunday
> AND it is the first sunday. It will ALSO return true if today is
> any day during the fourth week. I would think that the logic
> [ $dow -eq 0 -a $week -eq 4 ] should do the trick.
Because the OP asked "I want to start a script with a cronjob every fourth
sunday or the first sunday in the month."
--
Oh... sorry. Then you would want your if logic to be
if [ $dow -eq 0 -a \( $week -eq 1 -o $week -eq 4 \) ]
Otherwise it will execute on first sunday or any day in the fourth
week.