Help - Infinite Loop: Error in trap function


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Help - Infinite Loop: Error in trap function
# 1  
Old 02-25-2015
Wrench Help - Infinite Loop: Error in trap function

Hi,

I was working on implementing error handling in my bash scripts, and decided to use trap to send myself an email incase of any errors. But it seems that somethings has gone wrong, and I am continuously getting same emails for an old error repeatedly (even though I have stopped/killed all instances of the script).

Artifacts:
I am using a common script called: catch-errors.sh to catch errors:

catch-errors.sh
Code:
#!/bin/bash

set -o pipefail  # trace ERR through pipes
set -o errtrace  # trace ERR through 'time command' and other functions
set -o nounset   ## set -u : exit the script if you try to use an uninitialised variable
set -o errexit

function error_exit {
  LINE_NO=$2
  echo "`date` - Script $1 failed at line:${LINE_NO} with error-code:$3" | mail -s "Application XYZ failure" my-email-id@domain.com -- -f SenderName
  exit 2
}
PROG_NAME=$0
trap 'error_exit ${PROG_NAME} ${LINENO} $?' SIGHUP SIGINT SIGTERM ERR

</code>

Now, I am using above script in another main script for error handling:
main-script.sh
Code:
#!/bin/bash

. ./catch-error.sh

COUNT=0

while [ $COUNT -lt 10 ]
do
  --- do something --
  COUNT=$((COUNT+1))
done

echo "script completed."

Can someone please help.. how to stop the emails I am continuously getting?
I have killed any instance of above script, but still getting emails.
I have observed one thing - all the emails are for an old error since the timestamp being printed is the same old one, so I am guessing that maybe the trap function itself is throwing some error which is causing it to invoke itself again and again.
Code:
Wed Feb 25 15:07:31 PST 2015 - Script XYZ failed at line:16 with error-code:1

Any help would be appreciated..

Last edited by cool.aquarian; 02-25-2015 at 08:57 PM..
# 2  
Old 02-25-2015
Hi Aqua

To my understanding, the issue is behind the --- do something ---.
Though, havent worked with trap yet.

Not sure if you could 'add' a break to the trap, as in, break if trap gets raised?
(EDIT: just seeing now...) Confusing part is, you already got an exit 2 there....

hth
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read function is going in infinite in another script having while loop

Hello Experts, I have created one user confirmation process that will ask for user input. I have created one func for it. The issue is if i call it as normal then it works fine but if i am calling it in another script(in while loop) . It is going in infinite loop and not asking for user input. ... (8 Replies)
Discussion started by: looney
8 Replies

2. Shell Programming and Scripting

Infinite loop query

I have a script script.shwhich is scheduled to run at 11 AM everyday. # script.sh Code: ./scb_script.sh & unfortunately scb_script.sh is running today in infinite loop as respective files are not available. My question, when script.sh starts running tomorrow, will the old process be... (1 Reply)
Discussion started by: JSKOBS
1 Replies

3. Shell Programming and Scripting

How to stop infinite loop

Im unable to stop the below infinite loop (bash script). Can someone tell me why this isnt responding to signals eg: ctrl+c (SIGINT) or ctrl+z c=0 test_loop() { c=$(($c+1)) echo "count value is : $c " sleep 1 test_loop } Im using: SunOS 5.10 PS: If run this as... (13 Replies)
Discussion started by: Arun_Linux
13 Replies

4. Shell Programming and Scripting

My for loop decides to become an infinite loop?

Hi, I was debating if I should put this in the dummies or scripts section, I apologize in advance if I chose poorly. Fairly new to Unix and BASH scripting but I thought I made it fairly well given my limited understanding. However, the output indicates that it's looping and I'm ending up with a... (5 Replies)
Discussion started by: gotreef
5 Replies

5. Homework & Coursework Questions

Help with infinite loop problem

1. The problem statement, all variables and given/known data: My problem is an infinite loop when i press any other key other then Y or y in the while loop. what i want it to do is return to the normal script outside of it if pressing N or n or keep asking the same question if its any other... (4 Replies)
Discussion started by: Ren_kun
4 Replies

6. Shell Programming and Scripting

Call a infinite loop

Hi All, I need to run an infinite loop. requirement below: function1 --> creates a file file1 function2 ---> need to call if the file creates i am running these both function via a script --> script.sh i need to run the function1 first and if the file file1 creates then need to run the... (3 Replies)
Discussion started by: satyaranjon
3 Replies

7. Shell Programming and Scripting

Infinite while loop

what is the difference between while:,while true and while false? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

8. Shell Programming and Scripting

infinite while do loop problem

hi all, this is how my scrip looks like #!/bin/sh bindir='/opt/apps/script/bin' datadir='/opt/apps/script/data' dir='/opt/apps/script' while : ; do ls -1rt /opt/apps/script/data/check.txt*|tail -1 > /dev/null 2>&1 if ;then chmod +rwx $bindir/dummy2.sh ... (8 Replies)
Discussion started by: tententen
8 Replies

9. Programming

__read_nocancel Function Causes Infinite Loop

Does anyone know what __read_nocancel does and why it would go into an infinite loop? What I have gathered in my searches is that it pertains to server code. Yet, I'm not running this application in server mode. NOTE: There are server functions in the shared object, but the specific code... (3 Replies)
Discussion started by: marcus121
3 Replies

10. Solaris

ls command in infinite Loop

Hi, whenever I am giving a 'ls' command system is going into infinite loop displaying the current home directory. There is no separate shell script/file with ls name anywhere in the system. I am using Solaris 10. Any help / guidance in solving this problem is highly appreciated. ... (3 Replies)
Discussion started by: umakant
3 Replies
Login or Register to Ask a Question