The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How the first script should notify in case there is no response from second rajusa10 Shell Programming and Scripting 4 03-19-2008 01:35 PM
Notify Command. venkyA UNIX for Dummies Questions & Answers 2 08-21-2007 06:20 AM
Capturing last command execution status in a script. videsh77 Shell Programming and Scripting 2 04-12-2007 01:19 PM
Script to e-mail me when a command is done rocker40 UNIX for Dummies Questions & Answers 2 01-15-2004 06:31 PM
process pager - by cell phone or just e-mail notify squash UNIX for Advanced & Expert Users 9 10-23-2001 03:34 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-15-2008
mbak mbak is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 18
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 08:43 PM.. Reason: typo
  #2 (permalink)  
Old 07-15-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
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 (permalink)  
Old 07-15-2008
mbak mbak is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 18
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 (permalink)  
Old 07-15-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
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 (permalink)  
Old 07-15-2008
mbak mbak is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 18
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 08:52 PM..
  #6 (permalink)  
Old 07-15-2008
Annihilannic Annihilannic is offline Forum Advisor  
  
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 1,009
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 (permalink)  
Old 07-16-2008
mbak mbak is offline
Registered User
  
 

Join Date: Jan 2008
Posts: 18
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0