cancel
Showing results for 
Search instead for 
Did you mean: 

Cron

Isaac_4
Frequent Advisor

Cron

Hi:

I need find a solution for cron tasks y need send a email when some tasks fail only when fail

someone see this case

thks
The time is gold
4 REPLIES
Michal Kapalka (mikap)
Honored Contributor

Re: Cron

Steven E. Protter
Exalted Contributor

Re: Cron

Shalom,

This needs to be handled inside the cron script.

cron itself can email not at all, or every time the script runs.

So build this into the script cron calls:


# Get the return code from the important process
rc=$?
#
if [ $rc -eq 0 ]
then
echo "Process succeeded"
else
echo "Process failed | mailx -s "Job blah blah failed" yourmailbox@yourdomain.com
fi

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Isaac_4
Frequent Advisor

Re: Cron

But if used in my script the default error exit , that i believe its "/sbin/myscript 2> mailx -s "" "" etc. can work that ?
The time is gold
James R. Ferguson
Acclaimed Contributor

Re: Cron

Hi:

A 'crontab' job will generate a mail to the controlling account whenever *either* STDOUT or STDERR is produced and the output isn't redirected.

Well-behaved processes (and scripts) produce their desired output to STDOUT and any warnings or errors to STDERR. Thus, if this were the case for your process, you could suppress mail generation, except for error conditions, with:

# /usr/local/bin/mything > /var/tmp/mything.log

...assuming that if there is a warning or error, that information would be written to the un-redirected STDERR which would then trigger a mail event under 'cron'.

The success or failure of a process as cataloged in its return code has nothing to do with weather or not STDERR (or for that matter, STDOUT) might be written. You need to know what rules your process follows.

Regards!

...JRF...