Sujet : Cleaning up background processes
De : naddy (at) *nospam* mips.inka.de (Christian Weisgerber)
Groupes : comp.unix.shellDate : 05. May 2024, 21:06:22
Autres entêtes
Message-ID : <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
User-Agent : slrn/1.0.3 (FreeBSD)
Is there a standard POSIX shell idiom to clean up background
processes?
You have a shell script that starts some background process with &.
Now you want to make sure that the background process terminates
when the shell script terminates. In particular, when it terminates
due to special circumstances.
A noninteractive shell doesn't use job control, so the background
process shares the same process group. That's great! When somebody
hits ^C, SIGINT is sent to the whole process group. The same for
^\ and SIGQUIT, and for SIGHUP when the modem hangs up^W^W^Wxterm
is closed. So the background process will be terminated by default.
Except... bash seems to block SIGINT for background processes. As
does FreeBSD's sh with both SIGINT and SIGQUIT. What now?
Also, the shell script is typically invoked from some implementation
of make(1), which seems to add more complications.
This seems like a sufficiently common problem that there must be a
standard solution.
-- Christian "naddy" Weisgerber naddy@mips.inka.de