Subject: Re: exec redirect prevents trap
From: [email-masked]
To: Bart Schaefer <[email-masked]>
Cc: [email-masked]
Date: Sat, 16 Apr 2016 12:13:21 -0700
List-Id: Zsh Workers List <zsh-workers.zsh.org>

On Sat, Apr 16, 2016 at 09:34:33AM -0700, Bart Schaefer wrote:
> On Apr 16, 12:59am, [email-masked] wrote:
> }
> }     #!/bin/zsh
> }     exec > >(tee -a /tmp/foo)
> }     trap "echo hi" INT TERM EXIT
> }     sleep 1d;
> } 
> } When I run it and hit ^C, it doesn't print anything. But when I remove
> } the "exec" line and do the same, it prints "hi".
> 
> I suspect what you have here is a race condition.  When zsh exits,
> it first sends a HUP signal to all its children, including the tee
> process.  If tee dies before the exit trap runs, the "hi" will go
> nowhere.
> 
> Try one of these:
> 
>     exec > >(trap '' HUP; tee -a /tmp/foo)
> 
>     exec > >(tee -a /tmp/foo &! )
> 

The second one works, thank you. Not sure why the first one doesn't
work for me...

In my script, I had something like:

    trap "echo hi; rm -f bar" INT TERM EXIT

and I noticed that 'bar' wasn't getting removed. But changing it to

    trap "rm -f bar; echo hi" INT TERM EXIT

works, so I guess that must also have been a problem with 'tee'
exiting.

My apologies, I guess this was more of a zsh-users question...

Thank you,

Frederick