zsh-workers
 help / color / mirror / code / Atom feed
* exec redirect prevents trap
@ 2016-04-16  7:59 frederik
  2016-04-16 16:34 ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: frederik @ 2016-04-16  7:59 UTC (permalink / raw)
  To: zsh-workers

Dear Zsh Workers,

I have a script like this:

    #!/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 would have expected
it to print "hi" both times.

- is this a bug?

- is there a way to get the trap to execute, even with the exec line
  present?

Thank you,

Frederick Eaton


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: exec redirect prevents trap
  2016-04-16  7:59 exec redirect prevents trap frederik
@ 2016-04-16 16:34 ` Bart Schaefer
  2016-04-16 19:13   ` frederik
  0 siblings, 1 reply; 3+ messages in thread
From: Bart Schaefer @ 2016-04-16 16:34 UTC (permalink / raw)
  To: frederik, zsh-workers

On Apr 16, 12:59am, frederik@ofb.net 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 &! )


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: exec redirect prevents trap
  2016-04-16 16:34 ` Bart Schaefer
@ 2016-04-16 19:13   ` frederik
  0 siblings, 0 replies; 3+ messages in thread
From: frederik @ 2016-04-16 19:13 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: zsh-workers

On Sat, Apr 16, 2016 at 09:34:33AM -0700, Bart Schaefer wrote:
> On Apr 16, 12:59am, frederik@ofb.net 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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-04-16 19:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-16  7:59 exec redirect prevents trap frederik
2016-04-16 16:34 ` Bart Schaefer
2016-04-16 19:13   ` frederik

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).