AIX cron job is running everyday instead of on a particular day

Hello,

I was trying to run a script on a dev server using cron job. It supposed to run 3rd sunday of every month.

45 4 15-21 * 0 /home/user1/myscript.sh >/dev/null 2>&1 # run this script 3rd sunday of every month

When I Schedule it on AIX server, It is running every day at 4:45 AM.
am I doing anything wrong ?

I am thinking to try below, please suggest.

45 4 15-21 * 7 /home/user1/myscript.sh >/dev/null 2>&1 # run this script 3rd sunday of every month

But It is same as "0" for sunday.

How about below ?

45 4 15,16,17,18,19,20,21 * 0 /home/user1/myscript.sh >/dev/null 2>&1 # run this script 3rd sunday of every month

Please suggest.

Thanks,
Kumar

Yes, I fell over the some 20 years ago with the same result. It seems that cron does not make sure that all conditions for the day match, so you may find that it runs every day between 15th & 21st each month AND every Sunday.

You would think that what you have set up is perfectly sensible, but sadly the cron designers had other ideas.

I got round this by running a shell script everyday that decided if I was on the correct day and correct week, and if so then it progressed to run the script I really wanted. it is annoying and I sympathise, but that was my way round it.

From the crontab file manual (man 5 crontab) I get this:-

I hope that this helps,
Robin

1 Like

That means cron treats the dom field and the dow field as OR.
To make an AND, you put one field into the crontab and make your script exit on the other condition. For example

45 4 * * 0 /home/user1/myscript.sh >/dev/null 2>&1 # run this every sunday

And at the beginning of myscript.sh you put

# exit if day-of-month is not 15..21
dom=`date +%d`
[ $dom -ge 15 -a $dom -le 21 ] || exit
# the real script follows
...

Or, the other way round:

45 4 15-21 * * /home/user1/myscript.sh >/dev/null 2>&1 # run this every day 15..21

And at the beginning of myscript.sh you put

# exit if today is not Sunday
dow=`date +%w`
[ $dow -eq 0 ] || exit
# the real script follows
...
2 Likes

Thank you!