[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

trap bug ?

From: laura fairhead
Subject: trap bug ?
Date: Mon, 15 Apr 2002 22:40:06 GMT


I'm posting this from comp.unix.shell because a couple of us
think it might represent a bug in bash'es 'trap' command.

We've been through the documentation but cannot find any
explanation for the weird behaviour.

This full quote includes the original query, the test
has been run under the following systems so far;

bash version 2.04.0(1)-release (i386-suse-Linux)
bash version 2.03.0(1) (Solaris 2.7)


>> I am looking for help getting trap to work consistently in Bash 2.05 on
>> SuSE Linux.  Here is a sample script I put together to trap SIGHUP.  I can
>> see when I started it that the trap is active from the output of trap
>> -p.  However, when I send HUP to it (kill -1) it dies. Any ideas?
>> #!/bin/bash
>> #
>> # This is a sample daemon shell script. 
>> # Traps:
>> # signal HUP (1) re-read config file
>> #
>> conf=/tmp/daemon.conf
>> work=`cat $conf`
>> # set traps
>> trap 'work=`cat $conf`' 1
>> trap -p
>> # Loop forever
>> while :
>> do
>>     xmessage $work
>>     sleep 60
>> done
>> Best Regards,
>> <0
>> --
>> Free your mind, and your OS will follow
>> St

From: address@hidden (laura fairhead)
Newsgroups: comp.unix.shell
Subject: Re: Bash traps
Date: Mon, 15 Apr 2002 18:33:37 GMT
Message-ID: <address@hidden>
Reply-To: address@hidden

On Sun, 14 Apr 2002 21:06:52 GMT, Lessthan0 <address@hidden> wrote:

>I need to clarify this a bit.  My last post was not complete.
>When I just a single trap statement, it works fine.  I can send SIGHUP
>to it all day and it will run.  When I introduce a second trap on
>different signals, that's when the SIGHUP kills it.  For example:
># This is a sample daemon shell script.
># Traps:
># signal HUP (1) re-read config file
>work=`cat $conf`
>echo junk > $tempfile
># set traps
>trap 'work=`cat $conf`' 1
>trap 'rm -f $tempfile; exit' 0 15
>trap -p
># Loop forever
>while :
>    xmessage $work
>    sleep 60
>Now, does the second trap statement nullify the first one?  It appears
>that it should still work with signals 1 and 15 trapped and exit (0).
>However, signal 1 is NOT being trapped and I don't know why.
>Interestingly, when I kill it with kill -SIGHUP, the exit traps works and
>deletes the temp file.

It seems to me like setting an EXIT trap is clearing all previously
set traps for some reason (I couldn't see this documented in the
bash man-page).

I ran the following test program;

trap 'echo HUP'   HUP
trap 'echo USR1'  USR1
trap 'echo TERM'  TERM
trap 'echo EXIT'  EXIT
while :
  echo $$ working
  sleep 2

And rearranged the 'trap' lines for each of 4 placements of EXIT
(keeping the order of the other traps the same). Then I tried
sending each of the 3 signals to the program from another terminal
and recorded the result as to whether the trap was in effect or

trap       trap set                                              (1)
HUP        true
USR1       true
TERM       true

trap       trap set                                              (2)
HUP        false
USR1       true
TERM       true

trap       trap set                                              (3)
HUP        false
USR1       false
TERM       true

trap       trap set                                              (4)
HUP        false
USR1       false
TERM       false

So to work around this difficulty you can always put the EXIT trap
as the first one set in your code. I am still looking for an explanation
of why it behaves like this in the documentation,...


>Best Regards,


laura fairhead  # address@hidden  http://lf.8k.com
                # if you are bored crack my sig.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]