bug-bash
[Top][All Lists]
Advanced

[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

Hi,

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: laura@madonnaweb.com (laura fairhead)
Newsgroups: comp.unix.shell
Subject: Re: Bash traps
Date: Mon, 15 Apr 2002 18:33:37 GMT
Message-ID: <3cbb1cfd.260587@NeWs.CiS.dFn.De>
Reply-To: laura@madonnaweb.com

On Sun, 14 Apr 2002 21:06:52 GMT, Lessthan0 <me@privacy.net> 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:
>
>#!/bin/bash
>#
># This is a sample daemon shell script.
>#
># Traps:
># signal HUP (1) re-read config file
>#
>conf=~/daemon.conf
>work=`cat $conf`
>tempfile=/tmp/DAEMON1_$$
>echo junk > $tempfile
>
># set traps
>trap 'work=`cat $conf`' 1
>trap 'rm -f $tempfile; exit' 0 15
>
>trap -p
>
># Loop forever
>while :
>do
>    xmessage $work
>    sleep 60
>done
>####
>
>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;

#!/bin/sh
trap 'echo HUP'   HUP
trap 'echo USR1'  USR1
trap 'echo TERM'  TERM
trap 'echo EXIT'  EXIT
while :
do
  echo $$ working
  sleep 2
done

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
not;

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

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

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

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


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


byefrom

>
>Best Regards,
><0

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-- 
laura fairhead  # laura@madonnaweb.com  http://lf.8k.com
                # if you are bored crack my sig.
1F8B0808CABB793C0000666667002D8E410E83300C04EF91F2877D00CA138A7A
EAA98F30C494480157B623C4EF1B508FDED1CEFA9152A23DE35D661593C5318E
630C313CD701BE92E390563326EE17A3CA818F5266E4C2461547F1F5267659CA
8EE2092F76C329ED02CA430C5373CC62FF94BAC6210B36D9F9BC4AB53378D978
80F2978A1A6E5D6F5133B67B6113178DC1059526698AFE5C17A5187E7D930492



reply via email to

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