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
Redirect Standard Output Multi-Process djodjo High Level Programming 5 10-01-2008 05:09 AM
[BASH] redirect standard error and use it inside Pescator Shell Programming and Scripting 2 03-03-2008 09:20 AM
Question from a newbie. How to redirect standard output ndemos UNIX for Dummies Questions & Answers 1 07-27-2007 10:28 AM
redirect standard error into log file epall UNIX for Dummies Questions & Answers 7 05-09-2006 06:29 AM
Error: Internal system error: Unable to initialize standard output file firkus UNIX for Dummies Questions & Answers 2 10-25-2005 03:23 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 12-26-2007
barkath barkath is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 12
redirect only the standard error output to mail

I'm writing a script using file descriptor 2 (std error) to send an email only if the command fails or errors out but the script always emails me irrepective of whether it fails or not. It will not email the /tmp/check.error file output if doesn't error out just the mail with the subject "Cannot run check script on machB".

Any suggestions would be really appreciated. Thanks

###(Note: I have a mail problem with the one on 4th line, the other one works OK)
==========================================================================================
#!/bin/ksh

echo "" >> /tmp/sync.error

rsync -goptvz -e ssh /var/sync.all machB:/var/sync.all 2>>/tmp/sync.error

ssh -l root machB '/usr/local/check' 2>>/tmp/check.error | mail -s "Cannot run check script on machB" email@removed

if [ $? = 0 ]
then
date +"%D %T:$script Successfully executed." >> /dev/null

else
date +"%D %T:$script Unsuccessfully executed." >> /tmp/sync.error
date +"%D %T: Exiting script." >> /tmp/sync.error
mail -s "Cannot sync the sync.all file to machB, plz. look at /tmp/sync.error" email@removed < /tmp/sync.error

exit 1
fi

date +"%D %T: Sync of printcap.all file completed." >> /tmp/sync.error
  #2 (permalink)  
Old 12-26-2007
frank_rizzo frank_rizzo is offline Forum Advisor  
Resident BOFH
  
 

Join Date: Dec 2007
Posts: 427
A generic way to handle any errors the script would be to get rid of your return code logic and
put this at the top of your script. Remove the piped mail statement also.

Code:
trap 'mail -s "Error on line $LINENO; rc=$?" email@address </tmp/sync.error >/dev/null' ERR
or if you only care about the return from the ssh line... change

Code:
ssh -l root machB '/usr/local/check' 2>>/tmp/check.error | mail -s "Cannot run check script on machB" email@removed
to
Code:
ssh -l root machB '/usr/local/check' 2>>/tmp/check.error || mail -s "Cannot run check script on machB" email@removed </tmp/check.error >/dev/null
and remove your if test

Last edited by frank_rizzo; 12-26-2007 at 09:30 PM..
  #3 (permalink)  
Old 12-27-2007
barkath barkath is offline
Registered User
  
 

Join Date: Mar 2005
Posts: 12
redirect only the standard error output to mail

Thanks, I have used the trap command and it works good for me, also I brought down the code to a few lines, just wondering how the return code rc=$? values can be evaulated.
  #4 (permalink)  
Old 02-08-2008
internetlarry internetlarry is offline
Registered User
  
 

Join Date: Feb 2008
Location: Georgia, USA
Posts: 1
redirect only the standard error output to mail

Trying to setup rsync backup in the same fashion, but having much more limited knowledge than you gentlemen about shell scripts, can either of you give me an example of the 'finished' script you are talking about that is fully functional?

I am setting up a server that will use the rsync command and ssh to backup from local directories to an account at rsync.net and need to have emails sent to me if it fails.

Thanks.
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 05:40 AM.


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