Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: TRAPEXIT doesn't get executed



On Nov 7, 10:35am, Matt Armstrong wrote:
> Subject: TRAPEXIT doesn't get executed
>   
> ---------------------------------------------------------
> #!/usr/bin/zsh
>   
> set -x
>   
> function TRAPEXIT {
>     print "executed TRAPEXIT"
> }
> sleep 10
> ---------------------------------------------------------
>   
> And hit Ctrl-C while "sleep 10" is executing, the TRAPEXIT isn't
> executed.

This is a bit subtle, and I'm not entirely sure zsh is doing the right thing,
but what's happening here is that because zsh is running a script (rather
than interactively), it has not ignored SIGINT.  So the SIGINT is actually
being received by zsh and the c-runtime-library default SIGINT handler is
being called, which kills the process; there's no opportunity for zsh to run
the exit trap.

This is done in part so that shell loops that execute nothing but builtin
commands can nevertheless be interrupted by signals.  However, it might be
that zsh should trap SIGINT anyway and simply respond to it by exiting.

> ---------------------------------------------------------
> #!/usr/bin/zsh
>   
> set -x
> 
> function TRAPEXIT {
>     print "executed TRAPEXIT"
> }
> function TRAPINT {
>     print "executed TRAPINT"
> }
> sleep 10
> ---------------------------------------------------------
> 
> And hit Ctrl-C, both TRAPINT and TRAPEXIT get executed.

In this case, you've explicitly overridden the default SIGINT handler by
declaring a SIGINT trap function, so zsh *doesn't* exit on the SIGINT;
instead it exits by falling off the end after "sleep" is killed (by that
same signal -- the tty driver delivers it to all the processes in the tty
process group).  Put another "print" after the sleep in both scripts and
try again, and you'll see that in the second case the script is still
running after the interrupt, but in the first case it's not.



Messages sorted by: Reverse Date, Date, Thread, Author