Deutsch English Français Italiano |
<v197mk$1tr6h$7@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.nobody.at!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: vallor <vallor@cultnix.org> Newsgroups: comp.unix.shell Subject: Re: Cleaning up background processes Date: Mon, 6 May 2024 00:19:32 -0000 (UTC) Organization: A noiseless patient Spider Lines: 60 Message-ID: <v197mk$1tr6h$7@dont-email.me> References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Mon, 06 May 2024 02:19:33 +0200 (CEST) Injection-Info: dont-email.me; posting-host="b06b76e9fef9a4b156c4d134c0584b77"; logging-data="2026705"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18pmCPgpOpfISVKG7YiW8SE" User-Agent: Pan/0.158 (Avdiivka; aa34dd6 gitlab.gnome.org/GNOME/pan.git; x86_64-pc-linux-gnu) Cancel-Lock: sha1:I9aaHnYqDFMxgIbh5FIXWLASUiI= X-Face: \}2`P"_@pS86<'EM:'b.Ml}8IuMK"pV"?FReF$'c.S%u9<Q#U*4QO)$l81M`{Q/n XL'`91kd%N::LG:=*\35JS0prp\VJN^<s"b#bff@fA7]5lJA.jn,x_d%Md$,{.EZ Bytes: 3158 On Sun, 5 May 2024 19:06:22 -0000 (UTC), Christian Weisgerber <naddy@mips.inka.de> wrote in <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>: > 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. I have scripts that need to kill off processes started with "&", and there's a couple ways to do it. One way is "kill -1 0" -- sending the HUP signal with "0" as the process number will send the signal to the process group. Another way is to remember the process, such as: sleep 1000000 & # backgrounded process BACKGROUND=$! And then set a trap: trap "kill -1 $BACKGROUND" 0 You can get even fancier with something like: hang_up_the_phone() { HPID=$1; kill -0 $HPID > /dev/null 2>&1 && kill -1 $HPID; } trap "hang_up_the_phone $BACKGROUND" 0 I have a script that uses both, if you'd like me to post it -- it runs xdaliclock in timer mode, as well as a sleep for the length of the timer. For that, the trap looks like this: trap "hang_up_the_phone $BACK1 ; \ hang_up_the_phone $BACK2 ; " 0 CHLD; -- -v