LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Trapping Signals and Interrupts (https://www.linuxquestions.org/questions/linux-general-1/trapping-signals-and-interrupts-853945/)

devUnix 01-03-2011 12:49 PM

Trapping Signals and Interrupts
 
Hi,


I have a script that traps signals (interrupts) while it is executing. But when the Pause / Break keay or Ctrl + z keys combination is pressed then the script gets sent in background.

Here is the sample script:
Code:

[devarish@m-net ~]$ cat sig.sh
#!/bin/bash
trap "echo Abnormal Termination; exit 1" 2 3
while true; do
doNothing=0
done
exit 0

And here are some runs of the above script:

Code:

[devarish@m-net ~]$ ./sig.sh
^CAbnormal Termination
[devarish@m-net ~]$ ./sig.sh
^\Abnormal Termination
[devarish@m-net ~]$ ./sig.sh
^Z
[3]+  Stopped                ./sig.sh
[devarish@m-net ~]$ ./sig.sh
^Z
[4]+  Stopped                ./sig.sh
[devarish@m-net ~]$

Actually it is ^Z or Ctrl + z for Pause / Break key that needs to be trapped.

I have gone through this document:

http://www.tutorialspoint.com/unix/u...nals-traps.htm

but could not figure which among the following Signals would be equivalent to Ctrl + Z or ^Z:

Code:

[devarish@m-net ~]$ kill -l
 1) SIGHUP      2) SIGINT      3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGEMT      8) SIGFPE
 9) SIGKILL    10) SIGBUS      11) SIGSEGV    12) SIGSYS
13) SIGPIPE    14) SIGALRM    15) SIGTERM    16) SIGURG
17) SIGSTOP    18) SIGTSTP    19) SIGCONT    20) SIGCHLD
21) SIGTTIN    22) SIGTTOU    23) SIGIO      24) SIGXCPU
25) SIGXFSZ    26) SIGVTALRM  27) SIGPROF    28) SIGWINCH
29) SIGINFO    30) SIGUSR1    31) SIGUSR2
[devarish@m-net ~]$

Any ideas?

devUnix 01-03-2011 01:43 PM

Okay, I have got it. It is the Signal 18:

Code:

[devarish@m-net ~]$ cat sig.sh
#!/bin/bash
trap "echo Abnormal Termination; exit 1" 18
while true; do
doNothing=0
done
exit 0

A sample run:

Code:

[devarish@m-net ~]$ ./sig.sh
^ZAbnormal Termination
[devarish@m-net ~]$



All times are GMT -5. The time now is 08:59 AM.