Operating System - HP-UX
1830350 Members
1807 Online
110001 Solutions
New Discussion

shell script for the cron job failure

 
SOLVED
Go to solution
Sanjiv Sharma_1
Honored Contributor

shell script for the cron job failure

Hi All,

I am looking for a shell script which reads log file (/var/adm/cron/log) and it will e-mail the error message to the me, if there are any "rc" errors or the cron fails.

I want to schedule this script to run once a day and hence it should look for the crons which has run on the same day only.

Does anyone has a readymade script or give me some tips to get started.

Thanks,
Everything is possible
8 REPLIES 8
Paula J Frazer-Campbell
Honored Contributor

Re: shell script for the cron job failure

Hi

Have a look at this thread :-

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xf0fc5dc05a7ad711abdc0090277a778c,00.html


Paula
If you can spell SysAdmin then you is one - anon
Steve Steel
Honored Contributor

Re: shell script for the cron job failure

Hi

Add

cp /var/adm/cron/log /var/adm/cron/log.old
echo "" > /var/adm/cron/log

To the end of your job and then you only have cron since the last time your script ran

backup log.old later


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
F. X. de Montgolfier
Valued Contributor

Re: shell script for the cron job failure

Hi Sanjiv,

to make the script look only at the newer line in the log file, you can use the following:

1. initialise the file /tmp/croncheck:
echo 0>/tmp/croncheck

#!/usr/bin/ksh
j=`wc -l /var/adm/cron/log| awk '{print $1}'`
i=`cat /tmp/croncheck`
tail +$i /var/adm/cron/log >/tmp/tocheck
# run your error treatment job on /tmp/tocheck
echo $j >/tmp/croncheck
rm /tmp/tocheck

tail +$i will tail the /var/adm/cron/log file starting at line $i. This means that you get the last message from yersterday, which I think is a good idea.
If you truly want to start at the next line, you can use
tail +$(( $i + 1 )) /var/adm/cron/log >/tmp/tocheck

instead.

HTH,

FiX


Sanjiv Sharma_1
Honored Contributor

Re: shell script for the cron job failure

Hi,

Paula,
I didn't find any cron check script in the link given by you.

Steve,
Seems to be a good idea.

Fix,
# echo 0>/tmp/croncheck
# j=`wc -l /var/adm/cron/log| awk '{print $1}'`
# i=`cat /tmp/croncheck`
# tail +$i /var/adm/cron/log >/tmp/tocheck
tail: cannot open input
error: No such file or directory on file +
#

I am getting the error mentioned above.

Thanks,
Everything is possible
Con O'Kelly
Honored Contributor
Solution

Re: shell script for the cron job failure

Hi Sanjiv

Could try the following:

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

Cheers
Con
someone_4
Honored Contributor

Re: shell script for the cron job failure

Hi,


The user who runs the cron jobs get detailed emails from root.You can always setup .forward in / to send the emails wherever you want to. The emails have alot of great info about the script and why it failed.


If you dont want the information for every cron script you can do it per job.You can also redirect the output and email your self standard error and output.

Use this on the to of the script
exec > /home/richard/file.log 2>@1

And this at the bottom.
cat /home/richard/file.log | sendmail you@whereever



Richard




F. X. de Montgolfier
Valued Contributor

Re: shell script for the cron job failure

Hi Sanjiv,

> Fix,
> # echo 0>/tmp/croncheck
> # j=`wc -l /var/adm/cron/log| awk '{print $1}'`
> # i=`cat /tmp/croncheck`
> # tail +$i /var/adm/cron/log >/tmp/tocheck
> tail: cannot open input
> error: No such file or directory on file +
> #

looks like $i is not set. Could you echo $i and see if it's actually got a value? When I run the commands, I have:

# echo 0 > /tmp/croncheck
# cat croncheck
0
# j=`wc -l /var/adm/cron/log| awk '{print $1}'`
# echo $j
56662
# i=`cat /tmp/croncheck`
# echo $i
0
# tail +$i /var/adm/cron/log >/tmp/tocheck
# wc -l /tmp/tocheck
56662 /tmp/tocheck
# echo $j >/tmp/croncheck
# cat croncheck
56662


I forgot to use the full path for tail in my script: it should be /usr/bin/tail for use in cron, of course.

Are you using the korn shell? The script is written for ksh, and may not work with other shells...

HTH,

FiX


RolandH
Honored Contributor

Re: shell script for the cron job failure

Hi Sanjiiv,

as I know, everytime a cronjob failed the system creates a mail for the local root account.

Know I think you can configure sendmail on your system that this mails will be forwarded to your email account. You can do this very simple with the aliases file in /etc/mail/aliases.

put this in alaiases file.

root: sanjiiv@mydomain.com

Now you will get all emails from root also to your account.

Or do this.

root: "| This initiates a script you have written by yourself and will be started every time root gets a mail.

( from man page aliases:
.....
| command-line sendmail pipes the message as standard input to the specified command. If command-line contains blanks, it must be enclosed in quotation marks (").
For example,
msgs: "|/usr/bin/msgs -s"
......
)


Regards
Roland

Perhaps another way for solving your problem.

Sometimes you lose and sometimes the others win