Re: Cleaning up background processes

Liste des GroupesRevenir à cu shell 
Sujet : Re: Cleaning up background processes
De : vallor (at) *nospam* cultnix.org (vallor)
Groupes : comp.unix.shell
Date : 06. May 2024, 02:19:32
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v197mk$1tr6h$7@dont-email.me>
References : 1
User-Agent : Pan/0.158 (Avdiivka; aa34dd6 gitlab.gnome.org/GNOME/pan.git; x86_64-pc-linux-gnu)
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

Date Sujet#  Auteur
5 May 24 * Cleaning up background processes17Christian Weisgerber
5 May 24 +- Re: Cleaning up background processes1Lawrence D'Oliveiro
5 May 24 +- Re: Cleaning up background processes1Christian Weisgerber
6 May 24 +- Re: Cleaning up background processes1vallor
6 May 24 `* Re: Cleaning up background processes13Kaz Kylheku
6 May 24  `* Re: Cleaning up background processes12Kenny McCormack
8 May 24   +* Re: Cleaning up background processes9vallor
9 May 24   i+* Re: Cleaning up background processes4Janis Papanagnou
10 May 24   ii`* Re: Cleaning up background processes3Geoff Clare
10 May 24   ii `* Re: Cleaning up background processes2Janis Papanagnou
19 May 24   ii  `- Re: Cleaning up background processes1Martijn Dekker
11 May 24   i`* Re: Cleaning up background processes4Christian Weisgerber
11 May 24   i `* Re: Cleaning up background processes3Kenny McCormack
12 May 24   i  `* Re: Cleaning up background processes2Christian Weisgerber
13 May 24   i   `- Re: Cleaning up background processes1Geoff Clare
11 May 24   `* Re: Cleaning up background processes2Christian Weisgerber
11 May 24    `- Re: Cleaning up background processes1Kenny McCormack

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal