Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
TRAPCHLD and wait
- X-seq: zsh-workers 52363
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: TRAPCHLD and wait
- Date: Sun, 3 Dec 2023 10:02:12 -0800
- Archived-at: <https://zsh.org/workers/52363>
- List-id: <zsh-workers.zsh.org>
The manual says:
It is possible to wait for recent processes (specified by process
ID, not by job) that were running in the background even if the
process has exited. Typically the process ID will be recorded by
capturing the value of the variable $! immediately after the
process has been started.
This does work in the most obvious cases:
% { sleep 10; exit 23 } & bang=$!
[1] 560524
% sleep 20
[1] + exit 23 { sleep 10; exit 23; }
% wait $bang
% print $?
23
%
However, you can't do that wait in a CHLD trap, even though it still
works after the trap has run:
% TRAPCHLD() {
wait $bang
q=$?
zle -I
print $q
zle -R
}
% { sleep 10; exit 21 } & bang=$!
[1] 560661
%
[1] + exit 21 { sleep 10; exit 21; }
%
TRAPCHLD:wait:1: pid 560661 is not a child of this shell
127
% wait $bang
% print $?
21
%
I suspect this has to do with order of operations in the signal
handlers but thought I'd ask for clues before digging in myself.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author