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

Tags
aix, cron, job, running

 
Thread Tools Search this Thread
# 1  
Old 09-17-2018
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.
Code:
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.

Code:
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 ?

Code:
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

Last edited by Kumar7997; 09-17-2018 at 11:52 AM.. Reason: adding the code
# 2  
Old 09-17-2018
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:-
Quote:
Note: The day of a commandís execution can be specified by two fields ó day of month, and day of week. If both fields are restricted (ie, arenít *), the command will be run when
either field matches the current time. For example,
"30 4 1,15 * 5" would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.

I hope that this helps,
Robin
This User Gave Thanks to rbatte1 For This Post:
Kumar7997 (09-17-2018)
# 3  
Old 09-17-2018
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
Code:
45 4 * * 0 /home/user1/myscript.sh >/dev/null 2>&1 # run this every sunday

And at the beginning of myscript.sh you put
Code:
# 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:
Code:
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
Code:
# exit if today is not Sunday
dow=`date +%w`
[ $dow -eq 0 ] || exit
# the real script follows
...


Last edited by MadeInGermany; 09-17-2018 at 02:01 PM..
These 2 Users Gave Thanks to MadeInGermany For This Post:
Kumar7997 (09-17-2018) rbatte1 (09-18-2018)
# 4  
Old 09-17-2018
Thank you!

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
CRON job still running? rdogadin Shell Programming and Scripting 1 10-11-2013 03:13 PM
Cron job running even after cron is removed chidori Solaris 4 11-08-2012 11:05 AM
Run A Job in Cron On A Specific Day Excluding Holidays sktkpl UNIX for Advanced & Expert Users 1 09-04-2012 09:06 AM
Adding a cron job that will execute everyday at 14:50 hrs saurabhkoar Shell Programming and Scripting 2 07-03-2012 07:36 AM
Cron job is not running SunilB2011 Solaris 10 02-11-2012 01:25 AM
Cron is running,but my job is not triggering. mvennakula UNIX for Dummies Questions & Answers 1 01-09-2012 11:12 AM
Autosys Job Running every 5th Day of Month vbhatnag UNIX for Advanced & Expert Users 1 04-15-2011 12:05 PM
Cron job randomly once a day lucker Shell Programming and Scripting 6 11-01-2010 06:00 PM
Cron job running problem Asteroid Shell Programming and Scripting 3 05-30-2010 08:07 AM
Cron job not running yabhi_22 UNIX for Dummies Questions & Answers 7 07-04-2009 03:27 PM
cron job not running to completion novice633 UNIX for Dummies Questions & Answers 1 07-02-2009 07:43 PM
cron job is not running Suguna UNIX for Advanced & Expert Users 3 09-28-2006 10:11 AM
Cron job entry not running hkapil Shell Programming and Scripting 1 11-21-2005 10:46 AM
Cron Job Not Running nag_sundaram HP-UX 1 10-17-2005 11:38 AM
errors when running a cron job knarayan UNIX for Dummies Questions & Answers 2 01-23-2002 02:00 AM