bash and signals

bash and signals

Post by Steven Gallache » Sat, 09 Oct 1999 04:00:00

To all,

I have a small problem that is causing major headaches on a number of
scripts that I am attempting to run.  These scripts have worked correctly in
the past on other *nixes (DG/UX SunOS Solaris) using the standard sh shell.

The following script is a test script that emulates the process capability I
require from the production scripts.  Basically, these are launched into the
background, where they run and loop continuously, with long sleep periods
between each itteration.    The problem is that they do not react to signals
until the wait command exits, which is not the desired effect.

------- Start Script --------




trap KillApp TERM
trap KillApp USR1


while [ "$ShutdownSignal" = "N" ]; do
  echo "Top of the loop....."

  echo "Sleeping for 20 seconds..."
  sleep 20 &
  while [ "$ShutdownSignal" = "N" -a `ps -p $sleeppid | wc -l` -eq 2 ]; do
    wait $sleeppid >/dev/null 2>/dev/null

  [ "$ShutdownSignal" = "Y" -a `ps -p $sleeppid | wc -l` -eq 2 ] && kill
$sleeppid >/dev/null 2>/dev/null


echo "We're history........."
exit 0
----- END SCRIPT ------

The problem is that the wait call is not being interrupted by a TERM or USR1
signal (as I would have expected).  Is this a "feature" of bash, or is there
something going wrong.


Session 1

trap -- 'KillApp' SIGUSR1
trap -- 'KillApp' SIGTERM
Top of the loop.....
Sleeping for 20 seconds...
Top of the loop.....
Sleeping for 20 seconds...

Session 2
*ROOT* [blitzen]# kill -TERM 5039
*ROOT* [blitzen]#

The script running in session 1 does not quit from the loop until the sleep
completes.  Whereas I would expect the wait to be interrupted and the sleep
terminated with a kill command.

Example 2

[1] 5067

trap -- 'KillApp' SIGTERM
Top of the loop.....
Sleeping for 20 seconds...

Fri Oct  8 16:46:09 BST 1999

Fri Oct  8 16:46:17 BST 1999

Fri Oct  8 16:46:24 BST 1999
[1]+  Done                    sh ./

As can be seen from this example, the app did not complete for a number of
seconds after the kill was issued.

----- Start of Sample trace from Sun OS 5.6 /bin/sh -------


16: KillApp
Top of the loop.....
Sleeping for 20 seconds...
Top of the loop.....
Sleeping for 20 seconds...

Fri Oct  8 17:01:12 BST 1999
10982 Terminated
We're history.........
Fri Oct  8 17:01:13 BST 1999
Fri Oct  8 17:01:14 BST 1999

----- End of Sample trace from Sun OS 5.6 /bin/sh -------

On SunOS 5.6, the problem does not appear to exist as the script terminates
as soon as the signal is sent.

Before someone points out that that a 20 second pause is not a long time to
wait, I agree.  however, a 900 or 1200 second wait is a different matter.

I have a standard RedHat 6.0 installation, with the kernel upgraded to
I have tested this with bash versions
and the problem appears to exist in both.

If anyone has a solution to this problem, I will be most grateful.  If not,
I will raise a report with bash (which I may do anyway).  If anyone can
provide a pointer as to where they think the problem may be I will look into

I will also read the source code over the weekend to see if I can spot what
is happening.

Many thanks for any help.


Steven Gallacher
Replying to this message will NOT reach me.

J C Designs Ltd



Is there a proper fixed bash on any of the FTP sites out there?

I know there bash is on the usual sites but I don't know if they are
bugged or not :(




------------------------------------| Edinburgh, EH14 2DE, United Kingdom
**Domino: There`s nothing you can do when you`re the next in line: Domino**

2. Who Cares?

3. Matrox Mystique ands X.

4. Red Hat Linux for $50 at RedHat.Com vs. Red Hat from LSL

5. Bashing bash (Was Re: bash or user error with set -e and subshells)

6. E-Mail Problem

7. Bash calls TCL, TCL calls Bash, 2nd Bash never reads input

8. Can you decode this server error?

9. Problem trapping signals inside bash scripts

10. bash and Irix signal handling

11. Problem trapping ERR fake signal under bash

12. Signal # in bash script

13. bash ignoring all signals on sourced script?