You are not logged in.

#1 2011-05-26 09:40:02

daJense
Member
Registered: 2009-05-17
Posts: 45

Bash script - send mail only if script causes an error

Hi,
I have a script running on a server for backup purposes. I use

/bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE

to send a report at the end of the script. But I would like it to send the report only if the script errors out.

Any ideas,

Thx

Offline

#2 2011-05-26 09:46:25

Damnshock
Member
From: Barcelona
Registered: 2006-09-13
Posts: 414

Re: Bash script - send mail only if script causes an error

That depends on how you capture errors and how you call the script you are running. If you don't "exit" on errors, at least set a variable like "error" and, at the end of your script use something like:

if [[ error ]] ; then
    /bin/mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
fi


My blog: blog.marcdeop.com
Jabber ID: damnshock@jabber.org

Offline

#3 2011-05-26 10:03:15

daJense
Member
Registered: 2009-05-17
Posts: 45

Re: Bash script - send mail only if script causes an error

Thanks, I will try setting the variable. Btw the script is being called by another bashscript executing via cron on another server.

Offline

#4 2011-05-27 04:13:49

anrxc
Member
From: Croatia
Registered: 2008-03-22
Posts: 833
Website

Re: Bash script - send mail only if script causes an error

heirloom-mailx (new default mailx in Arch) supports -E, it won't send an e-mail if the body is empty. Good for cron.


You need to install an RTFM interface.

Offline

#5 2011-05-27 20:28:36

briest
Member
From: Katowice, PL
Registered: 2006-05-04
Posts: 468

Re: Bash script - send mail only if script causes an error

Bash (and probably other shells) has builtin command "trap" -- maybe you'd find it useful.

Offline

Board footer

Powered by FluxBB