bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

jobs builtin inside SIGCHLD trap doesn't clean up terminated jobs


From: Guldrelokk
Subject: jobs builtin inside SIGCHLD trap doesn't clean up terminated jobs
Date: Tue, 25 Apr 2023 21:41:57 +0300 (MSK)

POSIX has the following to say with respect to the 'jobs' utility:
When jobs reports the termination status of a job, the shell shall remove
its process ID from the list of those known in the current shell
execution environment
With Bash, this is not the case for 'jobs' called from within a SIGCHLD
handler.  This can be verified with:
sh -c 'set -m; trap "jobs %cat; jobs %cat" CHLD; cat </dev/null & wait %cat; 
jobs %cat; jobs %cat'
Which prints the job's exit status three times (two times with POSIX mode
disabled or with another external command after 'wait').
The described behavior is useful because it helps with monitoring child
processes asynchronously. For example:
trap \
'if jobs %helper && ! jobs %helper
then
        # restart helper
fi >/dev/null 2>&1' CHLD
Because job tables are not inherited by subshells, capturing the output of
'jobs' requires using a named pipe, which is relatively cumbersome.
Note that PIDs, unlike jobs, are intractably race-prone and cannot be
relied upon.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]