[linux-l] trap-Kommandoausf?hrung

Jens Dreger jens.dreger at physik.fu-berlin.de
Mo Jun 7 23:06:02 CEST 2004


On Mon, Jun 07, 2004 at 09:20:39PM +0200, Axel Wei? wrote:
> Ich habe gerade folgendes probiert:
> 
> axel at rantanplan:~/test> trap 'touch trapped' 2 3
> axel at rantanplan:~/test> trap
> trap -- 'touch trapped' SIGINT
> trap -- 'touch trapped' SIGQUIT
> axel at rantanplan:~/test> find / <irgendwas> # mit CTRL-C abgebrochen
> axel at rantanplan:~/test> ls
> trapped
> axel at rantanplan:~/test> rm trapped
> axel at rantanplan:~/test> trap 'touch ./trapped' 2 3
> axel at rantanplan:~/test> trap
> trap -- 'touch ./trapped' SIGINT
> trap -- 'touch ./trapped' SIGQUIT
> axel at rantanplan:~/test> find / <irgendwas> # mit CTRL-C abgebrochen
> axel at rantanplan:~/test> ls
> 
> Bin auch ratlos.

Da muss man aufpassen. Sobald man ein "trap 'touch trapped' 2 3"
gesetzt hat, reicht es, NUR Ctrl-C zu druecken, und die Datei ist
da. Der trap wird ja fuer die aktuelle shell gesetzt:

dreger at smart:..belug/bash> trap 'touch trapped' 2 3
dreger at smart:..belug/bash> trap
trap -- 'touch trapped' SIGINT
trap -- 'touch trapped' SIGQUIT
dreger at smart:..belug/bash> ls
dreger at smart:..belug/bash>        [CTRL-C gedrueckt]
dreger at smart:..belug/bash> ls
trapped

Das gleiche passiert auch, wenn man ./trapped nimmt:

dreger at smart:..belug/bash> rm trapped 
dreger at smart:..belug/bash> trap 'touch ./trapped' 2 3
dreger at smart:..belug/bash> trap
trap -- 'touch ./trapped' SIGINT
trap -- 'touch ./trapped' SIGQUIT
dreger at smart:..belug/bash> 
dreger at smart:..belug/bash> ls
trapped

Der trap wird ausgefuehrt, wenn das signal an die shell geschickt
wird:

dreger at smart:..belug/bash> rm trapped 
dreger at smart:..belug/bash> kill -INT $$           

dreger at smart:..belug/bash> ls         
trapped

Nicht jedoch, wenn das signal bei einem ausgefuehrten Kommando
ankommt:

dreger at smart:..belug/bash> find / -name nosuchfile &
[1] 15384
dreger at smart:..belug/bash> kill -INT 15384
[1]+  Interrupt               find / -name nosuchfile
dreger at smart:..belug/bash> ls
dreger at smart:..belug/bash>

Die Loesung liegt wohl in der Formulierung [man bash]:

 When  a simple command other than a builtin or shell function is to
 be executed, it is invoked in a separate execution environment that
 consists of the following. [...]

 *  traps caught by the shell are reset to the values the inherited
    from the shell's parent, and traps ignored by the shell are
    ignored.
 [...]

Gruss,

Jens.




Mehr Informationen über die Mailingliste linux-l