Frog_1337
asked on
UNIX Script. Send email if failure
I have a script that is very simple. A series of commands each ending with && to make sure they finish before the next one is ran. The script works fine but I need to error handling to send an email if any of the commands fail.
run the commands one at a time and then check value of $? if not zero then the last command failed
ASKER
How would I implement that into the script? The script is formatted like such
command one &&
command two &&
command three &&
command four.
command one &&
command two &&
command three &&
command four.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for letting me know where i was mistaken. And for the error reporting I simply use that format for each command?
Like this? But with the proper email and such
# Remove the snapshot fslv11
/usr/sbin/snapshot -d /dev/fslv11
if [[ $? -ne 0 ]]; then
echo "Script $0 failed "| mailx -s "Error" recipient@domain.tld
exit
fi
# The script will sleep to give the OS time to delete the snapshot fslv11
sleep 60
if [[ $? -ne 0 ]]; then
echo "Script $0 failed "| mailx -s "Error" recipient@domain.tld
exit
fi
# The script will unmount the filesystem /sdi00_backup
unmount /sdi00_backup
if [[ $? -ne 0 ]]; then
echo "Script $0 failed "| mailx -s "Error" recipient@domain.tld
exit
fi
Like this? But with the proper email and such
# Remove the snapshot fslv11
/usr/sbin/snapshot -d /dev/fslv11
if [[ $? -ne 0 ]]; then
echo "Script $0 failed "| mailx -s "Error" recipient@domain.tld
exit
fi
# The script will sleep to give the OS time to delete the snapshot fslv11
sleep 60
if [[ $? -ne 0 ]]; then
echo "Script $0 failed "| mailx -s "Error" recipient@domain.tld
exit
fi
# The script will unmount the filesystem /sdi00_backup
unmount /sdi00_backup
if [[ $? -ne 0 ]]; then
echo "Script $0 failed "| mailx -s "Error" recipient@domain.tld
exit
fi
The first code block ("snapshot") is OK.
The second one ("sleep") is OK as well, but the check is unnecessary - the returncode of "sleep" is tested, which will always be zero.
The third bock ("umount") is also OK, like the first one.
The second one ("sleep") is OK as well, but the check is unnecessary - the returncode of "sleep" is tested, which will always be zero.
The third bock ("umount") is also OK, like the first one.
ASKER
Thank you!
ASKER
Thank you so much for your assistance.
You're always welcome - thx for accepting!
Here is a solution which makes the script a bit shorter and allows for more meaningful mail messages:
# Remove the snapshot fslv11
CMD="/usr/sbin/snapshot -d /dev/fslv11"
if ! Result=$("$CMD"); then
echo "$Result"| mailx -s "Command $CMD in Script $0 failed" recipient@domain.tld
exit
fi
# The script will sleep to give the OS time to delete the snapshot fslv11
sleep 60
# The script will unmount the filesystem /sdi00_backup
CMD="unmount /sdi00_backup"
if ! Result=$("$CMD"); then
echo "$Result"| mailx -s "Command $CMD in Script $0 failed" recipient@domain.tld
exit
fi
Here is a solution which makes the script a bit shorter and allows for more meaningful mail messages:
# Remove the snapshot fslv11
CMD="/usr/sbin/snapshot -d /dev/fslv11"
if ! Result=$("$CMD"); then
echo "$Result"| mailx -s "Command $CMD in Script $0 failed" recipient@domain.tld
exit
fi
# The script will sleep to give the OS time to delete the snapshot fslv11
sleep 60
# The script will unmount the filesystem /sdi00_backup
CMD="unmount /sdi00_backup"
if ! Result=$("$CMD"); then
echo "$Result"| mailx -s "Command $CMD in Script $0 failed" recipient@domain.tld
exit
fi