1753820 Members
9029 Online
108805 Solutions
New Discussion юеВ

flagging script failures

 
SOLVED
Go to solution
gary phipps
Frequent Advisor

flagging script failures

Hi All,

I have a cron job that executes daily and I'd like an email sent to a user if this job fails. Other than checking the return code in the cron log, what's the easiest way to do this?

There are about half a dozen commands in the script so I could get the value of $? and check each line has worked but there nust be a better way.

Thanks,

Gary.
10 REPLIES 10
John Palmer
Honored Contributor

Re: flagging script failures

You need to write a better script that checks the result codes of each command that it calls. Indeed you may want to change the logic so that if a command fails then recovery action is taken or simply subsequent commands aren't run.

The return code in the cron log is simply the code returned by the script. Depending on how that's written it may be the code returned from the last command run.

If you post the cron script, I'd be able to give you some more information.

Regards,
John
gary phipps
Frequent Advisor

Re: flagging script failures

Hi John,

it's quite a simple script that extracts lines from a number of data files and concatenates them into another file.
I could check each command individually but I was wondering if there was a way to globally trap errors. Something like an "on error then goto" function.
I also wondered if there was a way to flag the fact that the cron job has failed. I could create another job that scans the cron log but I was hoping there was a simpler way.

Regards,

Gary.
John Palmer
Honored Contributor

Re: flagging script failures

You'll have to check each individually, the shell doesn't have an 'on error' facility.

As with most other things in UNIX though, there are many ways that you can code though. For instance, you can do...


if [[ ${?} -eq 0 ]];
then

if
then

&&


if [[ ${?} -ne 0 ]];
then

if !
then

||

etc...

The || construct can be useful with an error handling function, for example:-

function failit {
commands here to send you a mail etc. Arguments can be supplied to the function to give additional information about the failure.
exit
}

command1 || failit "command1 failed"

command2 || failit "command2 failed"

Regards,
John
Robin Wakefield
Honored Contributor
Solution

Re: flagging script failures

Hi Gary,

If you know the cronjob will return a non-zero code on error, then you could set your crontab entry to look like this:

0 0 * * * yourscript || mailx -s "yourscript failed" first.last@company.com

Rgds, Robin
Ian Dennison_1
Honored Contributor

Re: flagging script failures

On error? Theres a VB command I haven't heard for a while!

Put the entire main portion inside a function; at each major step, increment a position counter variable and export it. When you reach an error, perform a 'return' or 'break'.

Outside the function, test the value of the position counter; if it is not the final code (1 more than the variable for the last major step), display an error.

POSIX Shell programming has restrictive features, but some of them can be useful if you think a little laterally.

Share and Enjoy! Ian
Building a dumber user
Christian Gebhardt
Honored Contributor

Re: flagging script failures

Hi
look at "man crontab":

-----------------
WARNINGS
Be sure to redirect the standard output and standard error from
commands. If this is not done, any generated standard output or
standard error is mailed to the user.

---------------
So cron sends mail by default if you don't use redirection:

* * * * * command 1>/tmp/command.out

means that standard-out is written to file and standard-error sends a mail to the user of the cron

Chris
gary phipps
Frequent Advisor

Re: flagging script failures

Thanks guys,

I think I'll go with Robin's suggestion, putting "||" in crontab.

Regards,

Gary.

p.s. Ian, I was thinking DCL not VB!
Jean-Louis Phelix
Honored Contributor

Re: flagging script failures

Hi,

Perhaps I come too late, but I just want to add that a 'on error' like functionnality really exists in Posix shell. I never used it because I don't find it 'clean' enough, but ...

trap "echo error in script; exit 1" ERR

manages a special trap to gives this message and exit 1 whenever a command returns a non zero exit code. See man sh-posix, trap command.

Regards
It works for me (┬й Bill McNAMARA ...)
John Meissner
Esteemed Contributor

Re: flagging script failures

in your crontab couldn't you just output your errors?

00*** script 2> mail (etc....)

I've never tried this... i was wondering if this would work?
All paths lead to destiny