Using mail command to notify the status of script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using mail command to notify the status of script
# 1  
Old 07-15-2008
Using mail command to notify the status of script

Can someone please help me with this script, I'm trying to create system backup on AIX, for this I want to first mount the filesystem if it is not mounted, then create the backup and unmount the filesystem but I'm having problem while using the mail command to notify the status of filesystem whether it can be mounted or not, it doesn't send an email and doesn't append anything to the log files, also how do I calculate the time the script is taking to complete, is there a command I can use within the script ?

#!/usr/bin/ksh
set -x
HOSTNAME=$(hostname)
fs=$(df|grep testfs | awk '{print $7}')
if [ -z "$fs" ]; then
echo "Mounting filesystem..."
mount /testfs
mail -s "Filesystem mounted with success" -c "xxx@email1 yyy@email2"</var/log/succes.err >/dev/null
else
mail -s "No such filesystem exists on $HOSTNAME" -c "xxx@email1 yyy@email2"</var/log/miss.err >/dev/null

exit
fi

## If cannot mount then run,
if [ $? -ne 0 ]; then
mail -s "Cannot mount filesystem for $HOSTNAME" -c "xxx@email1 yyy@email2"</var/log/fail.err >/dev/null
exit
fi

Last edited by mbak; 07-15-2008 at 09:43 PM.. Reason: typo
# 2  
Old 07-15-2008
Does the mail command work on its own outside the script?

You could include the date command at the beginning and end of the script, or else use time scriptname to run it.

Incidentally, no need for awk and grep, this will do:

Code:
fs=$(df | awk '/testfs/ { print $7}')

# 3  
Old 07-15-2008
Using mail command to notify the status of script

Thanks for your quick response, mail command worked from the command line after removing the -c flag but does the script look OK bcos it still doesn't append the log files.
changed the mail command as below,
mail -s "Filesystem mounted with success" "xxx@email1 yyy@email2"</var/log/succes.err >/dev/null
# 4  
Old 07-15-2008
Maybe it should be success.err?

Also, another thing you will need to do is save the value of $? after the mount command, because it will change back to 0 when any subsequent command is run, such as mail for example. e.g. retcode=$? and if [ $retcode -ne 0 ]; then.
# 5  
Old 07-16-2008
Using mail command to notify the status of script

Should I save the value of $? after every command like mount, umount and use the code u suggested before each of the mail commands? Can u plz. be more descriptive ? Thanks again.

Last edited by mbak; 07-16-2008 at 09:52 PM..
# 6  
Old 07-16-2008
I would actually rearrange the code so that the test immediately follows the command. Note how you can just include a command in an if statement to test its success or failure:

Code:
#!/usr/bin/ksh
set -x
HOSTNAME=$(hostname)
fs=$(df| awk '/testfs/{print $7}')
if [ -z "$fs" ]; then
    echo "Mounting filesystem..."
    if mount /testfs; then
        mail -s "Filesystem mounted with success" "xxx@email1 yyy@email2"</var/log/succes.err >/dev/null
    else
        mail -s "Cannot mount filesystem for $HOSTNAME" "xxx@email1 yyy@email2"</var/log/fail.err >/dev/null
        exit 1
    fi
else
    mail -s "No such filesystem exists on $HOSTNAME" "xxx@email1 yyy@email2"</var/log/miss.err >/dev/null
    exit 2
fi

Also it is a good habit to exit n with an error code when your script is reporting an error.
# 7  
Old 07-16-2008
Using mail command to notify the status of script

I still have problem using the mail command within a script, I just want to send an email with the subject only.
On the command line for eg. :
# cat /etc/hosts |mail -s "Succesfully creatd mksysb for $HOSTNAME" "xxx@email1 yyy@email2" -->works

# mail -s "Succesfully creatd mksysb for $HOSTNAME" "xxx@email1 yyy@email2" -->doesn't work
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Notify as soon as an error is encountered in a script

Hi, The script below works okay and emails me the log in the end once the script completes but what I'm trying to do is to also notify me via an email as soon as the script encounters any error whatsoever. cat test.list hdisk0 00a6351a2c832da1 rootvg ... (7 Replies)
Discussion started by: mbak
7 Replies

2. Shell Programming and Scripting

Shell script to notify of service down

Hi All I am trying to write a shell script that will notify via email if a particular service is down. What I have so far is a script in cron like his: #!/bin/sh cd /usr/jdk/instances/jdk1.6.0/bin/sparcv9 jps -m And the output of the above is 81529 Jps 52988 TaskControllerService... (5 Replies)
Discussion started by: fretagi
5 Replies

3. Shell Programming and Scripting

How to get the exit status of a command in nner script to the outer script?

Hi all, I have a shell script inside which i am executing another shell script. In the inner script im executing a command. i want the status of that command in the outer script to perform some validations. How to get its status please help!!1 Im using ksh. (2 Replies)
Discussion started by: Jayaraman
2 Replies

4. Shell Programming and Scripting

Cronjob to notify by mail

I am planning some tasks that I need send mails regularly to my team members. I think to use cronjob with script to do it. Two init files are prepared, I can change the format if you ask for. name.txt: Bill bill@xxx.com Peter peter@xxx.com James james@xxx.com Tasks_list.txt ... (4 Replies)
Discussion started by: newoz
4 Replies

5. Shell Programming and Scripting

notify-send does not notify real time

Hi, I am having a little trouble getting notify-send to work the way I would like it to. I am using ubuntu - karmic koala 2.6.31-19-generic #56-Ubuntu SMP So here's the problem run the following commands one after the other. notify-send -i info -t 100000 -- "Hi" "world" & notify-send -i... (3 Replies)
Discussion started by: linuxpenguin
3 Replies

6. UNIX for Dummies Questions & Answers

unix script to check if rsh to box and send status mail

rshstatus=`rsh -n lilo /db/p2/oracle/names9208/restart_names.sh` if $rshstatus <>0 then errstatus=1 mailx -s "xirsol8dr" ordba@xxx.com >> $log_dr else if errstatus=0 echo "status to xirsol8dr successful" can anyone provide if this is t he correct way to do this or is there a better way? (1 Reply)
Discussion started by: bpm12
1 Replies

7. Shell Programming and Scripting

How the first script should notify in case there is no response from second

Hi Experts, I am trying to write a ksh script that it should notify in case there is no response from the other script. I mean to say that I got a.sh and b.sh the execution of b.sh depends on a.sh, so if there is no response from a.sh, b.sh should notify me about the same. Thanks in Advance (4 Replies)
Discussion started by: rajusa10
4 Replies

8. UNIX for Dummies Questions & Answers

Notify Command.

Hi, I am using the NOTIFY command and I am getting the emails.. I tried MAILX and e-mail is being sent. I want to use NOTIFY to see if my jobs are completed or not. Am I missing something.. All your ideas are greatly appreciated.. Thanks Venky (2 Replies)
Discussion started by: venkyA
2 Replies

9. Shell Programming and Scripting

Capturing last command execution status in a script.

How to capture output or numeric part given by $? command into the variable? If I go for var=`$?` then $var is found empty. (2 Replies)
Discussion started by: videsh77
2 Replies

10. UNIX for Advanced & Expert Users

process pager - by cell phone or just e-mail notify

hi all.. i need any help. i want to create in my crontab a simple script for verify any process and notify for the status in my e-mail or cell phone. anybody help me? (9 Replies)
Discussion started by: squash
9 Replies
Login or Register to Ask a Question