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


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
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 10:52 AM.. Reason: adding the code
# 2  
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:
# 3  
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 01:01 PM..
These 2 Users Gave Thanks to MadeInGermany For This Post:
Login or Register to Reply

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

More UNIX and Linux Forum Topics You Might Find Helpful
Cron job running even after cron is removed
chidori
Hi , I have removed a cron for particular user , but cron job seems to be running even after the cron entry is removed. The purpose of the cron was to sendmail to user ( it uses mailx utility ) I have restarted cron and sendmail service still user is getting mail alerts from the cron job. And...... Solaris
4
Solaris
Run A Job in Cron On A Specific Day Excluding Holidays
sktkpl
Hi, I want to run a job in cron on a specific date(say 25th of every month) excluding holidays. Can anyone provide some hints to do this? Thanks for any inputs.... UNIX for Advanced & Expert Users
1
UNIX for Advanced & Expert Users
Adding a cron job that will execute everyday at 14:50 hrs
saurabhkoar
Hi I want to execute a cron job everyday at 14:50 hrs. I have a script like this: TMP_FILE="/tmp/tmpcron.txt" RES=0 /usr/bin/crontab -l >> $TMP_FILE ADD_JOB="50 14 * * * /opt/mypath/scriptname.sh" grep "scriptname.sh" $TMP_FILE >> /dev/null JOB_NOT_EXIST=$? if test $JOB_NOT_EXIST...... Shell Programming and Scripting
2
Shell Programming and Scripting
Autosys Job Running every 5th Day of Month
vbhatnag
Hi, Is there a way to schedule a job in Autosys to run every 5th Day of Month without using custom calendar? Thanks.... UNIX for Advanced & Expert Users
1
UNIX for Advanced & Expert Users
Cron job randomly once a day
lucker
I want to create a cron job randomly once a day for my site's registration. The responsible file for registrations is a config file and I need to change the contents twice on day (on and off) I know the way for random cron job for example */n * * * * /usr/local/bin/php...... Shell Programming and Scripting
6
Shell Programming and Scripting