1846267 Members
5516 Online
110256 Solutions
New Discussion

Re: cron check script

 
SOLVED
Go to solution
Sanjiv Sharma_1
Honored Contributor

cron check script

Hi All,

Here is a script which sends an alert to me if the cronjob fails for today.

It has been observed that it doesn't give the cron status from Dates 2 to 9 but it works fine for the other dates. What needs to be done to make it work for dates 2 to 9?

Also I would like this script to give the cron status of only yesterdays cron jobs. What changes needs to be done for this?

*************
#!/usr/bin/ksh

admin1=ss1@abc.com
admin2=ss2@abc.com

host_name=`hostname`

DAY=$(date "+%a %b %d")
grep "rc=" /var/adm/cron/log | awk '/'"$DAY"'/ {print $0}' > /tmp/cronerrors.log

ERROR=`cat /tmp/cronerrors.log | wc -l`

if [ $ERROR -gt 0 ]
then
elm -s "ALERT!: $host_name: Cronjob failed !!" $admin1 elm -s "ALERT!: $host_name: Cronjob failed !!" $admin2 else
echo "Cronjob has run successfully"
fi
*******************

Thanks,
Everything is possible
22 REPLIES 22
Con O'Kelly
Honored Contributor

Re: cron check script

Hi Sanjiv

My guess is that if you look closely at the cron log all single digit days have a double space between the Month & Day, therefore awk is not matching anything for 1-9.

Example:
root 22368 c Sat Aug 2
root 13313 c Fri Aug 15

Not sure if it shows clearly here but there is a double space between "Aug" & "2".

You need to adjust the pattern match in awk to account for this. Hope that makes sense!


Cheers
Con
Sanjiv Sharma_1
Honored Contributor

Re: cron check script

Hi Con,

Yes. It makes sense. But how to adjust it to incorporates for the dates from 2 to 9.

I have received the cron status for date 1 but I am not sure how and why.

What about the cron status for the yesterday only?

Thanks,
Everything is possible
V.Tamilvanan
Honored Contributor
Solution

Re: cron check script

Hi Sanjiv,
I hope this script meets all your requirement. Why don't you try this.

#!/usr/bin/ksh

month=`date +%m`
day=`date +%d`
year=`date +%Y`

month=`expr $month + 0`

day=`expr $day - 1`

if [ $day -eq 0 ]; then

month=`expr $month - 1`

if [ $month -eq 0 ]; then
month=12
day=31
year=`expr $year - 1`
else
case $month in
1|3|5|7|8|10|12) day=31;;
4|6|9|11) day=30;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=29
elif [ `expr $year % 100` -eq 0 ]; then
day=28
else
day=29
fi
else
day=28
fi
;;
esac
fi
fi
case $month in
1) mon=Jan;;
2) mon=Feb;;
3) mon=Mar;;
4) mon=Apr;;
5) mon=May;;
6) mon=Jun;;
7) mon=Jul;;
8) mon=Aug;;
9) mon=Sep;;
10) mon=Oct;;
11) mon=Nov;;
12) mon=Dec;;
esac
x=`expr length "$day"`
if
[ $x -eq 1 ]
then
Day="${mon} ${day}"
else
Day="${mon} ${day}"
fi
cat /var/adm/cron/log|awk '/'"$Day"'/ && /'"$year"'/ && /'"rc="'/{print $0}' >/tmp/cronerr.log
mailx -s " Alert: Cron status" you@yourdomain.com < /tmp/cronerr.log


------------
HTH
Con O'Kelly
Honored Contributor

Re: cron check script

Hi
Very crude method, I'm sure others have a better idea:

//
DAY_NO=$(date +%d)
if [ $DAY_NO -lt 10 ]
then
DAY=$(date "+%a %b %d") # double space after %b
else
DAY=$(date "+%a %b %d")
fi

grep "rc=" ........ etc etc
//

Note the double space on first DAY variable setting in the "if" statement.

Haven't tested it but may work.

Cheers
Con
Sanjiv Sharma_1
Honored Contributor

Re: cron check script

Hi Con,

For DAY_NO less then 10 we get DAY as for
example:
# date "+%a %b %d"
Wed Aug 06

August 6 comes as 06 and as the /var/adm/cron/log doesn't has 06 pattern it ignores the croncheck of August 6.

How can I make the date to display as Wed Aug 6 and not as Wed Aug 06?

Here your double space option doesn't help.

Thanks,



Everything is possible
sdip
Advisor

Re: cron check script

Sanjiv,

Use %e instead of %d. It will give date output in character with leading space not like decimel.

So you need to execute DAY=$(date "+%a %b %e")to get proper output.

Cheers

Dip
Con O'Kelly
Honored Contributor

Re: cron check script

Hi

What about
DAY_NO=$(date +%d | sed 's/^0//')

Cheers
Con
Rob_132
Regular Advisor

Re: cron check script

date "+%a %b %e"

does not use the leading "0" on dates 1-9, but DOES use a leading space.

OR

date "+%a %b %d"|sed "s/\ 0/\ /g"

Maybe one of these will help.

Rob

Sanjiv Sharma_1
Honored Contributor

Re: cron check script

Hi Dip,

Yes. %e has helped.

Can we modify this script and make it to check the cron jobs which has run yesterday only?

Thanks,
Everything is possible
Rob_132
Regular Advisor

Re: cron check script

@#$%#$^!!!!

Thought I might have an answer worth points, but 2 people type faster than me!!

8-O
sdip
Advisor

Re: cron check script

Sanjiv,

Yes you can.

In your script just add following lines.

OLDTZ=`echo $TZ`
export TZ=MET+24 # It would set one day before date.
.....................
Execute all the commands...

In last line.
export TZ=$OLDTZ

Cheers

Dip
Sanjiv Sharma_1
Honored Contributor

Re: cron check script

Hi Dip,

I think this is dangerous.

When we execute
OLDTZ=`echo $TZ`
export TZ=MET+24 # It would set one day before date.

The date changes to the previous date and hence till the completion of the script it remains the same.

During this gap all the transation in the system will be as of yesterday's date. Don't you feel so.

Pls. correct me if I am wrong or else let us find out some other solution.

Thanks,
Everything is possible
Sanjiv Sharma_1
Honored Contributor

Re: cron check script

Hi Dip,

I think this is dangerous.

When we execute
OLDTZ=`echo $TZ`
export TZ=MET+24 # It would set one day before date.

The date changes to the previous date and hence till the completion of the script it remains the same.

During this gap all the transation in the system will be as of yesterday's date. Don't you feel so.

Pls. correct me if I am wrong or else let us find out some other solution.

Thanks,
Everything is possible
Sanjiv Sharma_1
Honored Contributor

Re: cron check script

Hi Dip,

I think it is dangerous.

OLDTZ=`echo $TZ`
export TZ=MET+24 # It would set one day before date.

This will set the date one day before and all the transation till the script executes fully will be as of yesterday's date.

Correct me of I am wrong or else let us look for some other method..

Thanks,
Everything is possible
Sanjiv Sharma_1
Honored Contributor

Re: cron check script

Hi Dip,

I think it is dangerous.

OLDTZ=`echo $TZ`
export TZ=MET+24 # It would set one day before date.

This will set the date one day before and all the transation till the script completes fully will be as of yesterday's date.

Correct me of I am wrong or else let us look for some other method..

Thanks,
Everything is possible
Sanjiv Sharma_1
Honored Contributor

Re: cron check script

Hi Dip,

I think it is dangerous.

OLDTZ=`echo $TZ`
export TZ=MET+24 # It would set one day before date.

This will set the date one day before and all the transation till the script completes fully will be as of yesterday's date.

Correct me if I am wrong or else let us look for some other method..

Thanks,
Everything is possible
Sanjiv Sharma_1
Honored Contributor

Re: cron check script

Hi Dip,

I think it is dangerous.

OLDTZ=`echo $TZ`
export TZ=MET+24 # It would set one day before date.

This will set the date one day before and all the transation till the script completes fully will be as of yesterday's date.

Correct me if I am wrong or else let us look for some other method..

Thanks,
Everything is possible
Con O'Kelly
Honored Contributor

Re: cron check script

Hi Sanjiv

I also use this TZ variable manipulation in my scripts. It shouldn't cause any problems. Don't export the TZ variable. Just set TZ back 24hours, then run your "date" commands and set TZ back to today.

EG:
TZ=EST+13EDT # Sets it back 24hrs
Run date commnads
TZ=EST-10EDT #Sets it to back to today

Cheers
Con
Sanjiv Sharma_1
Honored Contributor

Re: cron check script

Hi Con,

I have used the TZ variable as advised by you but the time doesn't goes 24 hrs back nor it comes back to the original time.

# echo $TZ
SST-8
# date
Fri Aug 15 15:51:18 SST 2003
# TZ=EST+13EDT
# date
Thu Aug 14 19:51:31 EDT 2003
# TZ=EST-10EDT
# date
Fri Aug 15 17:52:14 EST 2003
#

Thanks,
Everything is possible
Sanjiv Sharma_1
Honored Contributor

Re: cron check script

Well!!
Here is the final script which meets my requirement.

Thanks to all.
****************************
#!/usr/bin/ksh

admin1=ss1@abc.com
admin2=ss2@abc.com

host_name=`hostname`

TZ=MET+24

DAY=$(date "+%a %b %e")
grep "rc=" /var/adm/cron/log | awk '/'"$DAY"'/ {print $0}' > /tmp/cronerrors.log

ERROR=`cat /tmp/cronerrors.log | wc -l`

if [ $ERROR -gt 0 ]
then
elm -s "ALERT!: $host_name: Cronjob failed !!" $admin1 elm -s "ALERT!: $host_name: Cronjob failed !!" $admin2 else
echo "Cronjob has run successfully"
fi

TZ=SST-8
*************************************

Thanks again.
Everything is possible
Sanjiv Sharma_1
Honored Contributor

Re: cron check script

Hi All,

I have scheduled the above script to run once at 7:00 am every morning.

It has been found that it gives the cron report for day before yesterday.

How can I make it to give me the report of yesterday only?

Thanks,
Everything is possible
Con O'Kelly
Honored Contributor

Re: cron check script

Hi Sanjiv

I'd guess its a problem with your TZ definition.
I'm not sure why you use TZ=MET+24.
Assuming your current TimeZone is SST-8 then to go back 24hrs use TZ=SST+16.
Then set it back to current day with TZ=SST-8.

Cheers
Con