tell script to send success or failure email

Forum rules
Before you post please read how to get help
Post Reply
badams_ios
Level 2
Level 2
Posts: 71
Joined: Tue Jun 18, 2013 7:55 pm

tell script to send success or failure email

Post by badams_ios » Thu Mar 24, 2016 2:00 am

I have a script that runs once a day at 4:00 am. I have ssmtp setup and configured and when I run: sudo ssmtp me@mine.com it sends the message just fine so I know all that's working. What do I need to add to the script to get it to tell me if the script was successful or not?

The script DOES use exit errors 0 and 1. AND, it is successfully runs from cron.

And, again, I'm no big scripting genius. The script I run was made by someone else, with a very few modifications. See below.

Code: Select all

#! /bin/bash

#################### Begin Configuration #####################################

I left the configuration part out to shorten the reading as it's irrelevant. All the configuration does is mount drive shares.
I just need the script below to send an email when it succeeds or fails. 

#################### End Configuration #######################################


#################### Functions ###############################################

error_exit() {
    echo `date`
    exit 1
}

do_ifl_error() {
    if [ ! "$iflerror" = "0" ]; then
	echo "Image command $i failed with error code $iflerror"
	total_iflerror=1
    else
	echo "Image command $i completed without error"
    fi
}

##############################################################################

## start time stamp
echo `date`

## check for SMB mount program (smbmount or mount.cifs)
if [ "$protocol" = "smbfs" ]; then
    if [ ! -x /usr/bin/smbmount ]
	then echo "/usr/bin/smbmount not found, or not executable"
	error_exit
    fi
elif [ "$protocol" = "cifs" ]; then
    if [ ! -x /sbin/mount.cifs ]
	then echo "/sbin/mount.cifs not found, or not executable"
	error_exit
    fi
else
    echo "No valid protocol selected"
    error_exit
fi

## check for SMB umount program
if [ "$protocol" = "smbfs" ]; then
    if [ ! -x /usr/bin/smbumount ]
	then echo "/usr/bin/smbumount not found, or not executable"
	error_exit
    fi
else
    umountcifspath=""
    if [ -x /sbin/umount.cifs ]; then 
        umountcifspath="/sbin/umount.cifs"
    elif [ -x /bin/umount ]; then 
        umountcifspath="/bin/umount"
    else
        echo "both /sbin/umount.cifs and /bin/umount not found, or not executable"
        error_exit
    fi
fi
echo "Using the $protocol SMB protocol"

## check that mount point exists
if [ ! -d $mountpoint ]
    then echo "mount point $mountpoint does not exist, or is not a directory"
    error_exit
fi

## check if mount point already in use
mountpoint $mountpoint > /dev/null
if [ "$?" = "0" ]
    then echo "mount point $mountpoint already in use"
    error_exit
fi

## check if mount point is empty
am=`ls $mountpoint`
if [ -n "$am" ]
    then echo "mount directory $mountpoint not empty"
    error_exit
fi

## check if IFL path is valid and is executable
if [ ! -x $IFLpath ]
    then echo "$IFLpath not found, or not executable"
    error_exit
fi

## mount the share
if [ "$protocol" = "smbfs" ]; then
    smbmount //$server/"$share" $mountpoint $mountoptions
else
    /sbin/mount.cifs //$server/"$share" $mountpoint $mountoptions
fi    
if [ ! "$?" = "0" ]
    then echo "Failed to mount //$server/$share at $mountpoint"
    error_exit
else
    echo "Mounted //$server/$share at $mountpoint"
fi

## run the IFL command(s)
total_iflerror=0
echo "Number of image commands: $number_image_commands"     
if [ ! "$number_image_commands" = "0" ]; then
    for i in `seq 1 $number_image_commands`
    do 
	echo
	echo "Image command $i: ${iflmessage[$i]}"
	echo "$IFLpath ${iflcmd1[$i]} ${iflcmd2[$i]} ${iflcmd3[$i]}"
	if [ "$redirectnull" = "y" ]; then
	    $IFLpath ${iflcmd1[$i]} ${iflcmd2[$i]} ${iflcmd3[$i]} > /dev/null
	else
	    $IFLpath ${iflcmd1[$i]} ${iflcmd2[$i]} ${iflcmd3[$i]}
	fi
	iflerror=$?
	do_ifl_error
    done
    echo
fi

## unmount the share
sleep 1
for i in `seq 1 10`
do
    if [ "$protocol" = "smbfs" ]; then
	smbumount $mountpoint
    else
	$umountcifspath $mountpoint
    fi
    if [ ! "$?" = "0" ]
	then echo "Attempt $i: Failed to unmount //$server/$share from $mountpoint"
	if [ "$i" = "10" ]; then
	    error_exit
	else
	    sleep 2
	fi
    else
	echo "Unmounted //$server/$share from $mountpoint"
	if [ "$total_iflerror" = "0" ]
	    then echo `date` 
	    exit 0
	else
	    error_exit
	fi    
    fi
done
Last edited by xenopeek on Thu Mar 24, 2016 3:38 am, edited 1 time in total.
Reason: added code tag to preserve formatting and keep the topic readable
Clewless

Lenovo ThinkServer
Intel(R) Core(TM) i3-4130 CPU @ 3.40GHz
20 GB RAM
3 TB WD Black, hardware raid 5 - /home
Nvidia GeForce GT 220
32" TV
In the Man Cave :)


Mint Mate 17.1

Media Center, image and video editing

User avatar
cholq
Level 3
Level 3
Posts: 113
Joined: Tue Jul 08, 2014 9:27 pm
Location: New Jersey, USA

Re: tell script to send success or failure email

Post by cholq » Thu Mar 24, 2016 4:24 pm

You could write a 2nd script that calls the first and handles the emailing based on the first script's return value.

Code: Select all


#!/bin/bash

your_first_script_goes_here

if [ $? -eq 0 ]
then
  echo "Successfully ran script" | mail -s "script status" USERNAME@SOMEDOMAIN.COM
else
  echo "Could not run script"  | mail -s "script status" USERNAME@SOMEDOMAIN.COM
fi

Note: I don't have smtp set up on my machine, so those lines are just a guess based off of what I see online, but the code to check the return values should be good.

badams_ios
Level 2
Level 2
Posts: 71
Joined: Tue Jun 18, 2013 7:55 pm

Re: tell script to send success or failure email

Post by badams_ios » Fri Mar 25, 2016 7:06 pm

I think it matters exactly where that code goes. The job runs fine, but no message is being sent.

I test sending messages like this and it works. Please see the code from the script below. your code needs to capture the results at a different location in the script?

echo "Test message from Linux server using ssmtp" | sudo ssmtp -vvv brian@brianandmaryadams.com
[<-] 220 smtp.gmail.com ESMTP 20sm18583770pfj.80 - gsmtp
[->] EHLO gthycs@gmail.com
[<-] 250 SMTPUTF8
[->] STARTTLS
[<-] 220 2.0.0 Ready to start TLS
[->] EHLO gthycs@gmail.com
[<-] 250 SMTPUTF8
[->] AUTH LOGIN
[<-] 334 VXNlcm5hbWU6
[->] Z3RoeWNzQGdtYWlsLmNvbQ==
[<-] 334 UGFzc3dvcmQ6
[<-] 235 2.7.0 Accepted
[->] MAIL FROM:<gthycs@gmail.com>
[<-] 250 2.1.0 OK 20sm18583770pfj.80 - gsmtp
[->] RCPT TO:<brian@brianandmaryadams.com>
[<-] 250 2.1.5 OK 20sm18583770pfj.80 - gsmtp
[->] DATA
[<-] 354 Go ahead 20sm18583770pfj.80 - gsmtp
[->] Received: by gthycs@gmail.com (sSMTP sendmail emulation); Fri, 25 Mar 2016 15:59:25 -0700
[->] From: "root" <gthycs@gmail.com>
[->] Date: Fri, 25 Mar 2016 15:59:25 -0700
[->] Test message from Linux server using ssmtp
[->]
[->] .
[<-] 250 2.0.0 OK 1458946769 20sm18583770pfj.80 - gsmtp
[->] QUIT
[<-] 221 2.0.0 closing connection 20sm18583770pfj.80 - gsmtp

The code from the script I want to send the message is:

#! /bin/bash


##################### Start Configuration #####################################

export TERM=dumb

redirectnull=n


protocol=cifs

IFLpath=/tbu/imagel

server="192.168.1.63"

share="ManCaveSync"

mountpoint=/mnt

mountoptions="-o guest"

number_image_commands=1

iflcmd1[1]="--b"
iflcmd2[1]="--base:$mountpoint/Backups/myimage --f:$mountpoint/Backups/Backup-l1-chg-\$~YYYY\$-\$~MM\$-\$~DD\$-\$~HHMM\$ --comp:14"
iflcmd3[1]="--vb --uy --rb:4"
iflmessage[1]="Creating image myimage"
#################### End Configuration #######################################


#################### Functions ###############################################

error_exit() {
echo `date`
exit 1
}

do_ifl_error() {
if [ ! "$iflerror" = "0" ]; then
echo "Image command $i failed with error code $iflerror"
total_iflerror=1
else
echo "Image command $i completed without error"
fi
}

##############################################################################

## start time stamp
echo `date`

## check for SMB mount program (smbmount or mount.cifs)
if [ "$protocol" = "smbfs" ]; then
if [ ! -x /usr/bin/smbmount ]
then echo "/usr/bin/smbmount not found, or not executable"
error_exit
fi
elif [ "$protocol" = "cifs" ]; then
if [ ! -x /sbin/mount.cifs ]
then echo "/sbin/mount.cifs not found, or not executable"
error_exit
fi
else
echo "No valid protocol selected"
error_exit
fi

## check for SMB umount program
if [ "$protocol" = "smbfs" ]; then
if [ ! -x /usr/bin/smbumount ]
then echo "/usr/bin/smbumount not found, or not executable"
error_exit
fi
else
umountcifspath=""
if [ -x /sbin/umount.cifs ]; then
umountcifspath="/sbin/umount.cifs"
elif [ -x /bin/umount ]; then
umountcifspath="/bin/umount"
else
echo "both /sbin/umount.cifs and /bin/umount not found, or not executable"
error_exit
fi
fi
echo "Using the $protocol SMB protocol"

## check that mount point exists
if [ ! -d $mountpoint ]
then echo "mount point $mountpoint does not exist, or is not a directory"
error_exit
fi

## check if mount point already in use
mountpoint $mountpoint > /dev/null
if [ "$?" = "0" ]
then echo "mount point $mountpoint already in use"
error_exit
fi

## check if mount point is empty
am=`ls $mountpoint`
if [ -n "$am" ]
then echo "mount directory $mountpoint not empty"
error_exit
fi

## check if IFL path is valid and is executable
if [ ! -x $IFLpath ]
then echo "$IFLpath not found, or not executable"
error_exit
fi

## mount the share
if [ "$protocol" = "smbfs" ]; then
smbmount //$server/"$share" $mountpoint $mountoptions
else
/sbin/mount.cifs //$server/"$share" $mountpoint $mountoptions
fi
if [ ! "$?" = "0" ]
then echo "Failed to mount //$server/$share at $mountpoint"
error_exit
else
echo "Mounted //$server/$share at $mountpoint"
fi

## run the IFL command(s)
total_iflerror=0
echo "Number of image commands: $number_image_commands"
if [ ! "$number_image_commands" = "0" ]; then
for i in `seq 1 $number_image_commands`
do
echo
echo "Image command $i: ${iflmessage[$i]}"
echo "$IFLpath ${iflcmd1[$i]} ${iflcmd2[$i]} ${iflcmd3[$i]}"
if [ "$redirectnull" = "y" ]; then
$IFLpath ${iflcmd1[$i]} ${iflcmd2[$i]} ${iflcmd3[$i]} > /dev/null
else
$IFLpath ${iflcmd1[$i]} ${iflcmd2[$i]} ${iflcmd3[$i]}
fi
iflerror=$?
do_ifl_error
done
echo
fi

## unmount the share
sleep 1
for i in `seq 1 10`
do
if [ "$protocol" = "smbfs" ]; then
smbumount $mountpoint
else
$umountcifspath $mountpoint
fi
if [ ! "$?" = "0" ]
then echo "Attempt $i: Failed to unmount //$server/$share from $mountpoint"
if [ "$i" = "10" ]; then
error_exit
else
sleep 2
fi
else
echo "Unmounted //$server/$share from $mountpoint"
if [ "$total_iflerror" = "0" ]
then echo `date`
exit 0
else
error_exit
fi
if [ $? -eq 0 ]
then
echo "Good to go, baby!!!" | ssmtp brian@brianandmaryadams.com
else
echo "You're a failure!!!" | ssmtp brian@brianandmaryadams.com
fi
fi
done
Clewless

Lenovo ThinkServer
Intel(R) Core(TM) i3-4130 CPU @ 3.40GHz
20 GB RAM
3 TB WD Black, hardware raid 5 - /home
Nvidia GeForce GT 220
32" TV
In the Man Cave :)


Mint Mate 17.1

Media Center, image and video editing

Post Reply

Return to “Scripts & Bash”