Operating System - HP-UX
1833760 Members
2210 Online
110063 Solutions
New Discussion

Cron Entry - Please Advice.

 
Saraswathy_1
Advisor

Cron Entry - Please Advice.

Hello All,

I do not want to run /home/saras/dudd script on every first day of the month and saturday. so the crontab entry looks like below.

30 1 2-31 * 0-5 /home/saras/dudd

I could skip on saturday facing problem with first day of the month. Though I gave 2-31 still its running on 1st day of the month.
Iam using HP UNIX 11.11

Please advice in this matter.
14 REPLIES 14
Ermin Borovac
Honored Contributor

Re: Cron Entry - Please Advice.

Entries in the 3rd and 5th column in crontab are cumulative. So your cronjob will be run on 2-31 day of each month PLUS Sunday to Friday.

morganelan
Trusted Contributor

Re: Cron Entry - Please Advice.

Hi,
I think you must specify the corect day according to the month,so i think you must create 12 line job.For example at Year 2005:
jan :31 days
feb: 28 days
mar: 31 days
...
dec:31 days
so change your crontab file to:
0 1 2-31 1 0-5 /home/saras/dudd
0 1 2-28 2 0-5 /home/saras/dudd
....
0 1 2-31 12 0-5 /home/saras/dudd
Kamal Mirdad
Muthukumar_5
Honored Contributor

Re: Cron Entry - Please Advice.

Can you try like this,

30 1 * * 0-5 /home/saras/dudd 1>/dev/null 2>&1

in /home/saras/dudd put in the begin like,

if [[ $(date +'%d') -eq 1 ]]
then
echo "No need to run"
exit 0
fi

hth.
Easy to suggest when don't know about the problem!
Gavin Clarke
Trusted Contributor

Re: Cron Entry - Please Advice.

I suggest testing within the script (or as part of another script) for the first day of the month.

if [ `date | awk '{print $3}' | bc` -ne 1 ]
then
.
.
.
.
.
fi

Then the cron entry would look like this:

30 1 * * 0-5 /home/saras/dudd

That is the way I would do it.

There is probably a way to do it just using cron too.
Saraswathy_1
Advisor

Re: Cron Entry - Please Advice.

I regret for conveying wrong problem. The Actual problem -
Though I give 0-5 days the script executed on saturday. which I do not want.

30 1 2-31 * 0-5 /home/saras/dudd

Mr. Ermin Borovac, Could you please elaborate on this

"Entries in the 3rd and 5th column in crontab are cumulative. So your cronjob will be run on 2-31 day of each month PLUS Sunday to Friday".



Rick Garland
Honored Contributor

Re: Cron Entry - Please Advice.

Here is something that could help.
Call a script that has the logic built into it.

set `date +'%d %w'`
DAY=$1
WEEKDAY=$2

if [ "$WEEKDAY" != "6" ]
then
/home/saras/dudd
else
echo "Not running script"
exit
fi

Basically saying that this script checks the WEEKDAY and if it is SAT (6) then do not run dudds and exit.

The cron entry looks like
30 1 2-31 * *

This cron entry will execute the script everyday of the year except the 1st day of a month. The logic in the script says not to run 'dudd' on Saturdays.





Marvin Strong
Honored Contributor

Re: Cron Entry - Please Advice.

31 1 2-31 * * [[ `date +\%u` -ne 6 ]] && /home/saras/dudd

that should should run your script 2-31 of every month but not on saturdays.

Saraswathy_1
Advisor

Re: Cron Entry - Please Advice.

Hi Marvin,

Could you please tell how exactly the below statement is going to work. Just curious to know

[[ `date +\%u` -ne 6 ]] &&

Since the cron log shows :-

CMD: [[ `date +%u` -ne 2 ]] && date>/home/infra/dat

Does cron considers the above statement as single command ? Please clarify
Mark Grant
Honored Contributor

Re: Cron Entry - Please Advice.

Just in case Marvin doesn't get back to you, the construct "[[ some test ]] && some job" works out as "do some job if some test is successfull". The && makes the command on the right run if the exit status of the command on the left is 0. The opposite is ||
Never preceed any demonstration with anything more predictive than "watch this"
Marvin Strong
Honored Contributor

Re: Cron Entry - Please Advice.

As Mark stated, the test [[ `date +\%u -ne 2 ]]

executes the date command getting day of the week as 1 - 7 (sunday is 7) if that does not equal 2 then it runs the script. Thus it will be false on Tuedays and would not run the script. Since false is a non-zero return.

Cron runs entries in the Posix shell that is why the [[ ]] syntax works.
Saraswathy_1
Advisor

Re: Cron Entry - Please Advice.

Hello,

I just came up with one logic which is

[ `date +\%d` -ne 1 ]

Is this logic which Iam using is Korn shell specific.
Please Advice.
Muthukumar_5
Honored Contributor

Re: Cron Entry - Please Advice.

[ `date +%d` -ne 1 ] && will work as,

if [[ `date +$d` -ne 1 ]]
then
Only one expression;
fi

If you don't want to execute /home/saras/dudd script on first day of month simply do in the /home/saras/dudd script as,

[[ $(date +%d) -eq 1 ]] && exit 0;

in the begining of script.

hth.

Easy to suggest when don't know about the problem!
Eknath
Trusted Contributor

Re: Cron Entry - Please Advice.

Hi

This can be done in two ways
1. In crontab include dates 2-31 and modify the script in such a way that it does not run on Saturday. or

2. In crontab include the days except sat and modify the script to check the date and does not execute on 1st day of month...

Enjoy!!!
eknath
Muthukumar_5
Honored Contributor

Re: Cron Entry - Please Advice.

See man ksh for Conditional Expressions and [[ expression ]] part.

If you don't want to execute on first day of month and saturday of the week then,

30 1 * * * [[ $(date +%d) -ne 1 || $(date +%w) -ne 5 ]] && /home/saras/dudd 1>/dev/null 2>&1

hth.
Easy to suggest when don't know about the problem!