Sujet : Re: Cleaning up background processes
De : gazelle (at) *nospam* shell.xmission.com (Kenny McCormack)
Groupes : comp.unix.shellDate : 11. May 2024, 21:36:45
Autres entêtes
Organisation : The official candy of the new Millennium
Message-ID : <v1ohcd$rrf5$2@news.xmission.com>
References : 1 2 3 4
User-Agent : trn 4.0-test77 (Sep 1, 2010)
In article <
slrnv3v08i.17e3.naddy@lorvorc.mips.inka.de>,
Christian Weisgerber <
naddy@mips.inka.de> wrote:
On 2024-05-06, Kenny McCormack <gazelle@shell.xmission.com> wrote:
>
Having said that, I think we are all making our own assumptions about what
the actual, underlying problem is. Given that OP is not a newbie, it would
help a lot if he would clarify what exact situation he is dealing with,
rather than have us all guess (which is SOP when the poster *is* a newbie).
>
As part of a regression test suite, somebody wrote something like
this in a script:
>
./http-server &
trap "kill %1" HUP INT QUIT PIPE TERM
sleep 1 # server starts up
>
[... Tests ...]
>
kill %1
wait %1 # wait for http-server
>
Using job control syntax in a non-interactive shell is already wrong,
although, to my surprise, it works as intended in common shells.
Thanks for the followup. I agree that using job control syntax in a
(non-interactive) script is weird and to be avoided - even if it does "work".
It should be good enough to just store the value of $! and then use that
later on.
I also believe that there is no "silver bullet" solution to this problem.
There should be; it is shame that there isn't. You really should not even
find yourself in the situation of having to hunt down and kill wayward
processes when something unexpected happens in a script, but the fact is
you do.
For an example of this, I give you: sshfs. I love sshfs, but it is a fact
that if you try to sshfs-mount something that "isn't there" (for various
values of "isn't there"), it creates a mess that takes several steps to
undo (clean up). I have successfully tackled this problem, but it is
non-trivial, which is not unexpected, considering how many moving parts are
involved in an sshfs mount operation.
I've been looking at how to replace this with a portable-ish,
reliable-ish solution. An obvious step is to use a standard
background process reference $! instead of job control %1.
Less obvious is the question how to deal with signals that abort
the script, without leaving background processes hanging around.
Someone mentioned using prctl() and PR_DEATHSIG earlier. I think that's a
good idea, albeit not universal and, of course, Linux-specific. I think it
would work in your specific case of a wayward http server.
-- Faith doesn't give you the answers; it just stops you from asking the questions.