How to automatically send a mail alert if the script errors out


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to automatically send a mail alert if the script errors out
# 1  
Old 12-29-2009
Question How to automatically send a mail alert if the script errors out

I have a script like this, which calls other scripts from that script:

Code:
#!/usr/ksh

moveFiles.sh

extract.sh

readfile=/home/sample.txt

cat $readfile | while read line

do 

file= `echo $line|awk '{print $4}'`

if [ $file ];
       then mv $file /home/temp_stage
fi

done

I have to write the above script in such a manner that if it errors out any point, It has to automatically send an email notification. How do i find out if the file has errored out at any point.
I am struck at the point of... how to find if there is any error in the scripts that it calls...

Ur help is greatly appreciated.
Thanks in advance!

Last edited by Scott; 12-29-2009 at 12:02 PM.. Reason: Code tags, please!
# 2  
Old 12-29-2009
try something like this:
Code:
#!/usr/ksh
email_it()
{
   echo "This script had an error on line $1" | /usr/bin/mailx -s 'script error' me@mycomputer.com
   exit 1
}

moveFiles.sh  || email_t $LINENO
extract.sh || email_t $LINENO

readfile=/home/sample.txt
cat $readfile | while read line
do 
  file= `echo $line|awk '{print $4}'`

  if [ $file ];
  then 
        mv $file /home/temp_stage
  fi
done

This assumes the other scripts return non-zero (i.e., exit 1) on error.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 12-29-2009
Thanks Jim.

Let me try with that.
Can u please explain to me, what does this "||" stand for in unix and hoe does it work?
I have never used it before.
# 4  
Old 12-29-2009
The "||" means only run what is after the "||" if the code before the "||" fails, the opposite is "&&" which means only run what is after the "&&" if the code before the "&&" has succeeded, e.g.:
Code:
cp fred /var && rm fred

Only delete fred if it was successfully copied to /var.
This User Gave Thanks to TonyFullerMalv For This Post:
# 5  
Old 12-29-2009
Thanks Tony!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to send mail alert

Hi I have below shell script to send mail alert , however I want to add more functionality in this script and that is , script should only check that file between 9 am to 5pm , and if there is no activity in this time 9 am to 5 pm for 2hours then it should give me mail alert, please help... (2 Replies)
Discussion started by: scazed
2 Replies

2. Shell Programming and Scripting

Run a script before and after reboot automatically and send output to two locations.

Hello Team . I am working a health check script ( bash) to run on linux server ( RedHat) and requirements are 1. The o/p of script need to be send to two diff files . I am testing with tee command . But I am not successful yet , any recommendations if that is the right approach ? 2. The same... (2 Replies)
Discussion started by: Varja
2 Replies

3. Shell Programming and Scripting

Shell script to send mail alert

HI Guys, I am writing one shell script to send the mail alert to some email id's if the file not modified in last 10 mins but its not working, I believe MTIME is null string is wrong . can you please assist me on this. script :- filename="abc.txt" echo "Filename is $filename"... (1 Reply)
Discussion started by: abhigrkist
1 Replies

4. Shell Programming and Scripting

how to write a shellscript to send a mail alert to the website user based on license expiration time

hi, i am very much new to shell scripting i have a requirement that i have to develop a License Renewal Alert system that has to give a alert mail to the users before 30days of user account expiration, by checking expiration date of the user with the data base, this system will... (0 Replies)
Discussion started by: deepu_Shscripts
0 Replies

5. Shell Programming and Scripting

Poll for a file. If not present...Send a alert mail

Dear Experts, I have a requirement where a 3rd party system places a file in my folder.I am running on HP UNIX. I would like to have a unix script which will check for the existence of the file. If yes OK. if the file is not placed then it has to send a mail to couple of emails ids saying that... (3 Replies)
Discussion started by: phani333
3 Replies

6. Shell Programming and Scripting

How to use SSH to connect to Primary DB and send alert mail

Hi All, AIX 5.3 64 bit: I am using the below shell script the objective is: Objective: ======== Use SQL*Plus to query the MAX(SEQUENCE#) from both databases V$LOG_HISTORY view. If the STANDBY appears to be falling behind,then send alert mail through the below shell script: How... (0 Replies)
Discussion started by: a1_win
0 Replies

7. Shell Programming and Scripting

how to send mail automatically in unix

Dear friends, I am storing daily some data in a tape disk. and numbering it from 1 2 3 like this. I want to get alert mail automatically when ever data is greater than or equal to 600. to my yahoo mail. i want to put that mail script in crontab so it will check the data daily at... (7 Replies)
Discussion started by: rajan_ka1
7 Replies

8. Shell Programming and Scripting

need to send alert mail

hi, i need to monitor a cron job that runs every 5 mins. if this cron job does not process the request for more than 6 hrs, an alert mail should be sent. how do i achieve this?? (1 Reply)
Discussion started by: smurala
1 Replies

9. Linux

Unable to send mail - but no errors found :-(

Hi Guys I am using this version of Linux box (as shown below). I am unable to send email from the box. But I am not getting any errors while sending email. :mad: Any idea what could be the reason? What entry should I check? :confused: $ uname -a Linux machine-name 2.4.21-144-smp4G #1... (6 Replies)
Discussion started by: csaha
6 Replies

10. Shell Programming and Scripting

Send mail with attachments automatically in a interval period

hai everyone I want to send mail with attachments automatically for every 15 minutes getting different E-mail IDs from a file if any script is available , Please send to me .. This will be very useful for my project.... Thanks for your time..... Felix ... (3 Replies)
Discussion started by: Leo Felix
3 Replies
Login or Register to Ask a Question