Notify as soon as an error is encountered in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Notify as soon as an error is encountered in a script
# 1  
Old 10-22-2016
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.


Code:
cat test.list
hdisk0         00a6351a2c832da1                    rootvg          active
hdisk1         00a6351a2c832f66                    rootvg          active
hdisk2         00a6351a2c833311                    optvg           active
hdisk3         00a6351a2c8334a5                    optvg           active
hdisk4         00a6351a2cbf3049                    optvg           active
##########################################
cat NEWDISK.list
rootvg hdisk6 hdisk0
rootvg hdisk7 hdisk1
optvg  hdisk8 hdisk2
optvg  hdisk9 hdisk3
optvg  hdisk10 hdisk4

Code:
cat replacepv.sh
#!/bin/ksh
cat test.list | while read DISK1 PVID VG
 do
  if grep $DISK1 NEWDISK.list | read  VG DISK2
   then
        echo "Replace DISK $DISK1 to $DISK2........" | tee -a LOG.OUT
        sudo  replacepv $DISK1 $DISK2  | tee -a ERR.OUT
     fi
 done
echo "" | tee -a LOG.OUT
echo "" | tee -a ERR.OUT

mail -s "Disk replaced" user@abc.com< LOG.OUT

Thanks,
mbak

Last edited by mbak; 10-23-2016 at 03:48 AM..
# 2  
Old 10-23-2016
Quote:
Originally Posted by mbak
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.


Code:
cat test.list
hdisk0         00a6351a2c832da1                    rootvg          active
hdisk1         00a6351a2c832f66                    rootvg          active
hdisk2         00a6351a2c833311                    optvg           active
hdisk3         00a6351a2c8334a5                    optvg           active
hdisk4         00a6351a2cbf3049                    optvg           active
##########################################
cat NEWDISK.lst
rootvg hdisk6 hdisk0
rootvg hdisk7 hdisk1
optvg  hdisk8 hdisk2
optvg  hdisk9 hdisk3
optvg  hdisk10 hdisk4

Code:
cat replacepv.sh
#!/bin/ksh
cat test.list | while read DISK1 PVID VG
 do
  if grep $DISK1 NEWDISK.list | read  VG DISK2
   then
        echo "Replace DISK $DISK1 to $DISK2........" | tee -a LOG.OUT
        sudo  replacepv $DISK1 $DISK2  | tee -a ERR.OUT
     fi
 done
echo "" | tee -a LOG.OUT
echo "" | tee -a ERR.OUT

mail -s "Disk replaced" user@abc.com< LOG.OUT

Thanks,
mbak
Please be more explicit about what errors you want to cause to send an additional email.

Is it an error that you are invoking replacepv with three operands while the man page only specifies what happen when two operands are given? For example, when you read the 1st line from test.list (setting DISK1 to hdisk0), you will be running the command:
Code:
        sudo  replacepv hdisk0 hdisk6 hdisk0  | tee -a ERR.OUT

Is that an error? Does the above replacepv command complete successfully? Does it print any diagnostic messages? Does it write anything into ERR.OUT?

Is it an error when your grep command matches two or more lines and you only process one of those lines? For example, when you read the 2nd line from test.list, the command:
Code:
        grep hdisk1 NEWDISK.list

will match the following lines from NEWDISK.list:
Code:
rootvg hdisk7 hdisk1
optvg  hdisk10 hdisk4

but you only read the 1st matched line and run the command:
Code:
        sudo  replacepv hdisk1 hdisk7 hdisk1

ignoring the 2nd line and not running the command:
Code:
        sudo  replacepv hdisk1 hdisk10 hdisk4

Is that an error? Or, is it an error that the above grep command matched hdisk10 on the 2nd line when it should not match hdisk10 when I assume you only wanted exact matches for the word hdisk1?

Or, did you just want to mail any lines appended to ERR.OUT (even though ERR.OUT does not contain any diagnostic messages that might have been written to explain what errors had been detected)?
# 3  
Old 10-23-2016
My apologies for missing out a value in the script, it should have been as below,

Code:
if grep -w $DISK1 NEWDISK.list | read  VG DISK2 DISK3

Since I'm replacing multiple disks, I want to get an email or I can set it up to send an alert whenever the "replacepv" command fails for any reason like disk not found or something before trying to run "replacepv" on next disk in sequence in other words notify me as soon as it fails at any point during the script execution. I noticed that ERR.OUT doesn't log anything if I use a disk that doesn't exist which throws an error on screen.
# 4  
Old 10-23-2016
Please be more specific about what you're asking.

What system hardware are we talking about?
What RAID controller?

Are you asking....
How to interrogate the RAID controller?
How to trap the error in the script?

When you say to mail you as soon as it fails are you saying that you want to receive an email that very second? Email systems (daemons) don't work that fast. The mail relays don't work that fast depending if your inbox isn't on the same system. It could take a few minutes.

---------- Post updated at 09:38 AM ---------- Previous update was at 09:36 AM ----------

Oh, and which operating system is it?
# 5  
Old 10-23-2016
OS=AIX
Basically, I'm just trying to trap the error in the script and log it to ERR.OUT before mailing out ERR.OUT via mail command.
# 6  
Old 10-25-2016
So can't you test the exit status of the command to see if it's non-zero and, if so, email the log to yourself there and then?

I'm assuming that the RAID command obeys usual practice and gives a non-zero exit status if it errors.
This User Gave Thanks to hicksd8 For This Post:
# 7  
Old 10-25-2016
This may work, assuming I understand your requirements correctly.

Add the following lines to the start of your program:
Code:
err_code() {
mail -s "Problem with disk replacement" user@abc.com< ERR.OUT
}
trap 'err_code' ERR
set -e
set -o pipefail

What should now happen is that if the disk replacement program fails, an ERROR signal is sent to the process running your script, and it will run the function err_code, then exit.

The command set -e tells the shell to exit with an error if a command fails (you may want to put your loop, or just the sudo line, between a set -e set +e pair of commands so that other commands don't execute the err_code function). This isn't enough, however, as the tee command will exit with no error. So set -o pipefail command will cause the pipeline to fail if the sudo fails.

Hope that helps.

Andrew
This User Gave Thanks to apmcd47 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Problem with shell script while spaces encountered in directory names

Hi, I am having issues with the jar -tf command when I put in the shell script. The command runs fine from the command line as shown below. # jar -tf "./VirtualBox Dropped Files/2016-04-17T20:58:49.129139000Z/hive-exec-0.8.1.jar" But when I put in a shell script(shown below) and the... (10 Replies)
Discussion started by: vinoo128
10 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

awk - continue when encountered division error

Hello, How can I add a logic to awk to tell it to print 0 when encountering a division by zero attempted? Below is the code. Everything in the code works fine except the piece that I want to calculate read/write IO size. I take the kbr / rs and kbw / ws. There are times when the iostat data... (5 Replies)
Discussion started by: tommyd
5 Replies

4. Shell Programming and Scripting

Bash script to notify when ever any files are changed

Hi the following script let sthe user know whenevr any file is changed inserted or deleted in file system. but i am getting following error while running bash script ## LINUX SYSTEM FILE ARCHIVE NOTIFY ## if ; then echo "Usage '$0 folder waitseconds' " ; exit 1; fi if ; then echo "Folder... (1 Reply)
Discussion started by: programmingzeal
1 Replies

5. AIX

Error encountered in creating a filesystem

Hi experts, Need help on the below error please. I am creating Filesystem and it fails with the below errors :( Command: failed stdout: yes stderr: no Before command completion, additional instructions may appear below. 0518-506 odmget: Cannot open object class PdAt ... (10 Replies)
Discussion started by: EngnrRG
10 Replies

6. 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

7. Shell Programming and Scripting

How to write a shell script to notify when certain texts appear in a file?

I have a server and occasionally the file mysqld.log would show something like /usr/libexec/mysqld: Disk is full writing './example_com_-_wordpress/wp_statpress.MYD' (Errcode: 122). Waiting for someone to free space... Retry in 60 secs How do I write a simple shell script to check mysqld.log... (1 Reply)
Discussion started by: acider
1 Replies

8. Shell Programming and Scripting

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... (9 Replies)
Discussion started by: mbak
9 Replies

9. 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

10. UNIX Benchmarks

Encountered error!

I used this on an AIX machine and encountered the following error. $ ls -l total 600 -rwxrwxrwx 1 e26936 dba 1491 Feb 07 1992 MANIFEST -rwxrwxrwx 1 e26936 dba 8148 Apr 05 1992 Makefile -rwxrwxrwx 1 e26936 dba 4852 Sep 06 2003 README -rwxrwxrwx... (0 Replies)
Discussion started by: puspendu
0 Replies
Login or Register to Ask a Question