Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

"Thank heaven for startups; without them we'd never have any advances." -- Seymour Cray


devel / comp.unix.shell / Cleaning up background processes

SubjectAuthor
* Cleaning up background processesChristian Weisgerber
+- Re: Cleaning up background processesChristian Weisgerber
+- Re: Cleaning up background processesLawrence D'Oliveiro
+- Re: Cleaning up background processesvallor
`* Re: Cleaning up background processesKaz Kylheku
 `* Re: Cleaning up background processesKenny McCormack
  +* Re: Cleaning up background processesvallor
  |+* Re: Cleaning up background processesJanis Papanagnou
  ||`* Re: Cleaning up background processesGeoff Clare
  || `* Re: Cleaning up background processesJanis Papanagnou
  ||  `- Re: Cleaning up background processesMartijn Dekker
  |`* Re: Cleaning up background processesChristian Weisgerber
  | `* Re: Cleaning up background processesKenny McCormack
  |  `* Re: Cleaning up background processesChristian Weisgerber
  |   `- Re: Cleaning up background processesGeoff Clare
  `* Re: Cleaning up background processesChristian Weisgerber
   `- Re: Cleaning up background processesKenny McCormack

1
Cleaning up background processes

<slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8102&group=comp.unix.shell#8102

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.swapon.de!weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: naddy@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Cleaning up background processes
Date: Sun, 5 May 2024 19:06:22 -0000 (UTC)
Message-ID: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
Injection-Date: Sun, 5 May 2024 19:06:22 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="20340"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Sun, 5 May 2024 19:06 UTC

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

Re: Cleaning up background processes

<slrnv3fori.lnv.naddy@lorvorc.mips.inka.de>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8103&group=comp.unix.shell#8103

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!rocksolid2!news.nk.ca!weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: naddy@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Sun, 5 May 2024 19:52:18 -0000 (UTC)
Message-ID: <slrnv3fori.lnv.naddy@lorvorc.mips.inka.de>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
Injection-Date: Sun, 5 May 2024 19:52:18 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="22272"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Sun, 5 May 2024 19:52 UTC

On 2024-05-05, Christian Weisgerber <naddy@mips.inka.de> wrote:

> Except... bash seems to block SIGINT for background processes. As
> does FreeBSD's sh with both SIGINT and SIGQUIT. What now?

On repeat experiment, both bash and FreeBSD sh only block SIGINT
for background processes.

--
Christian "naddy" Weisgerber naddy@mips.inka.de

Re: Cleaning up background processes

<v18pp3$21u38$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8104&group=comp.unix.shell#8104

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.swapon.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ldo@nz.invalid (Lawrence D'Oliveiro)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Sun, 5 May 2024 20:21:56 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <v18pp3$21u38$1@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: Sun, 05 May 2024 22:21:56 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="33a041cd80492abf930d5e0c5f5526b9";
logging-data="2160744"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+c1Fi+R41yFrC5QIkaJBFE"
User-Agent: Pan/0.155 (Kherson; fc5a80b8)
Cancel-Lock: sha1:GP3CFse9f7+4ADx8jhamEXGtkGw=
 by: Lawrence D'Oliv - Sun, 5 May 2024 20:21 UTC

On Sun, 5 May 2024 19:06:22 -0000 (UTC), Christian Weisgerber wrote:

> 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.

There is no standard POSIX solution to this. There is a Linux-specific
solution, through the prctl(2) call
<https://manpages.debian.org/2/prctl.en.html>: in a child process, you
make a call with PR_SET_PDEATHSIG and specify a signal to be sent to
the child if/when the parent terminates, such as SIGKILL.

Note also the PR_SET_CHILD_SUBREAPER function: this is useful if a
child process spawns its own child and then dies; this allows the
subreaper process to get the notification when that child-of-a-child
terminates, instead of passing that buck back to pid 1.

Re: Cleaning up background processes

<v197mk$1tr6h$7@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8105&group=comp.unix.shell#8105

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!usenet.network!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: vallor@cultnix.org (vallor)
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
 by: vallor - Mon, 6 May 2024 00:19 UTC

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

Re: Cleaning up background processes

<20240505214609.114@kylheku.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8106&group=comp.unix.shell#8106

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.nk.ca!2.eu.feeder.erje.net!3.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Mon, 6 May 2024 04:46:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <20240505214609.114@kylheku.com>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
Injection-Date: Mon, 06 May 2024 06:46:59 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5bd9e627c6b08b4a27ba15e75a1e232b";
logging-data="2507308"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18zmXkb16ZWpSswIZ5yi0UwEeIY/QN95eY="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:zlJ8QBVU9g1saX/inm4ZTrHAmzo=
 by: Kaz Kylheku - Mon, 6 May 2024 04:46 UTC

On 2024-05-05, Christian Weisgerber <naddy@mips.inka.de> wrote:
> 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.

Maybe have an EXIT trap which calls wait?

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Cleaning up background processes

<v1ah87$l0a8$1@news.xmission.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8107&group=comp.unix.shell#8107

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!usenet.network!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!weretis.net!feeder9.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Mon, 6 May 2024 12:08:39 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <v1ah87$l0a8$1@news.xmission.com>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de> <20240505214609.114@kylheku.com>
Injection-Date: Mon, 6 May 2024 12:08:39 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="688456"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Mon, 6 May 2024 12:08 UTC

In article <20240505214609.114@kylheku.com>,
Kaz Kylheku <643-408-1753@kylheku.com> wrote:
>On 2024-05-05, Christian Weisgerber <naddy@mips.inka.de> wrote:
>> 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.
>
>Maybe have an EXIT trap which calls wait?

The fundamental underlying problem here is that the EXIT trap is only
called on a "normal" exit. In particular, it does not get called under (at
least) the following circumstances:

1) User hits ^C causing the script to abort.

2) Script exits via an "exec" statement.

As I read it, that's what this thread is actually about.

This is a problem I've often grappled with and I'm convinced that there is
no universal solution.

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).

--
Indeed, most .NET developers couldn't pass CS101 at a third-rate
community college.
- F. Russell -

Re: Cleaning up background processes

<v1gpdq$3me9$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8108&group=comp.unix.shell#8108

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: vallor@cultnix.org (vallor)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Wed, 8 May 2024 21:04:58 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <v1gpdq$3me9$1@dont-email.me>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
<20240505214609.114@kylheku.com> <v1ah87$l0a8$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 08 May 2024 23:04:58 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b4d3f200feaa5642c33106880b90c11a";
logging-data="121289"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2efP8e5U18xgFxaWvET1x"
User-Agent: Pan/0.158 (Avdiivka; cf43eb8; Linux-6.8.9)
Cancel-Lock: sha1:dyem2ZqdkR0h5e/Ykz781cQdqlY=
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
 by: vallor - Wed, 8 May 2024 21:04 UTC

On Mon, 6 May 2024 12:08:39 -0000 (UTC), gazelle@shell.xmission.com (Kenny
McCormack) wrote in <v1ah87$l0a8$1@news.xmission.com>:

> In article <20240505214609.114@kylheku.com>,
> Kaz Kylheku <643-408-1753@kylheku.com> wrote:
>>On 2024-05-05, Christian Weisgerber <naddy@mips.inka.de> wrote:
>>> 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.
>>
>>Maybe have an EXIT trap which calls wait?
>
> The fundamental underlying problem here is that the EXIT trap is only
> called on a "normal" exit. In particular, it does not get called under
> (at least) the following circumstances:
>
> 1) User hits ^C causing the script to abort.

I'm sorry, but I've waited and nobody said anything, so
I have to ask: Why couldn't you trap "kill -1 0" INT?

> 2) Script exits via an "exec" statement.

This is true, and I can't see any way around that.

> As I read it, that's what this thread is actually about.
>
> This is a problem I've often grappled with and I'm convinced that there
> is no universal solution.
>
> 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).

Wish they would clarify what they meant...

--
-v

Re: Cleaning up background processes

<v1h0hk$7rkr$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8109&group=comp.unix.shell#8109

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Thu, 9 May 2024 01:06:18 +0200
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <v1h0hk$7rkr$1@dont-email.me>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
<20240505214609.114@kylheku.com> <v1ah87$l0a8$1@news.xmission.com>
<v1gpdq$3me9$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 09 May 2024 01:06:28 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7f8e9c24e99186d404666d5481cd65b8";
logging-data="257691"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/eLRUVuK5fqet+NJ1RUENT"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:Htox6oItDWpv/jMv+IZbSRSF1KM=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <v1gpdq$3me9$1@dont-email.me>
 by: Janis Papanagnou - Wed, 8 May 2024 23:06 UTC

On 08.05.2024 23:04, vallor wrote:
> On Mon, 6 May 2024 12:08:39 -0000 (UTC), gazelle@shell.xmission.com (Kenny
> McCormack) wrote in <v1ah87$l0a8$1@news.xmission.com>:
>
>> In article <20240505214609.114@kylheku.com>,
>> Kaz Kylheku <643-408-1753@kylheku.com> wrote:
>>> On 2024-05-05, Christian Weisgerber <naddy@mips.inka.de> wrote:
>>>> 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.
>>>
>>> Maybe have an EXIT trap which calls wait?

'wait' wouldn't abort the process (as the OP seems to have required).

>>
>> The fundamental underlying problem here is that the EXIT trap is only
>> called on a "normal" exit. In particular, it does not get called under
>> (at least) the following circumstances:
>>
>> 1) User hits ^C causing the script to abort.
>
> I'm sorry, but I've waited and nobody said anything, so

I hadn't answered because it doesn't work as had been advertised. With

trap 'echo INT' INT
trap 'echo EXIT' EXIT

and hitting ^C I get both signals (INT) and pseudo-signal (EXIT)
in that order (with ksh, bash) [and it makes of course sense]

INT
EXIT

so it would be no problem, in my book.

But the pseudo-signal 'EXIT' is non-standard (to my knowledge), so you
cannot generally rely on it, depending on your environment, and the OP
asked for a "standard POSIX shell idiom".

> I have to ask: Why couldn't you trap "kill -1 0" INT?

Storing PIDs and killing the respective processes on exit is something
I had done in the past, making use of the process groups is something
I'd also consider; both things you already proposed upthread, IIRC.

Janis

>> [...]

Re: Cleaning up background processes

<ptg1hk-7n3.ln1@ID-313840.user.individual.net>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8110&group=comp.unix.shell#8110

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: geoff@clare.See-My-Signature.invalid (Geoff Clare)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Fri, 10 May 2024 13:35:37 +0100
Lines: 26
Message-ID: <ptg1hk-7n3.ln1@ID-313840.user.individual.net>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
<20240505214609.114@kylheku.com> <v1ah87$l0a8$1@news.xmission.com>
<v1gpdq$3me9$1@dont-email.me> <v1h0hk$7rkr$1@dont-email.me>
Reply-To: netnews@gclare.org.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net ZU4QZPygCFwZxrAurVQJrAm3hZRp3yqTAYiUbXopiTfRVItGnh
X-Orig-Path: ID-313840.user.individual.net!not-for-mail
Cancel-Lock: sha1:enqVs73UQlrzQIn2CUDxG5WGgBE= sha256:eq361OSBWemZ+5+daNx4vIg2AllaajbFvIhrEMIxUsk=
User-Agent: Pan/0.154 (Izium; 517acf4)
 by: Geoff Clare - Fri, 10 May 2024 12:35 UTC

Janis Papanagnou wrote:

> But the pseudo-signal 'EXIT' is non-standard (to my knowledge), so you
> cannot generally rely on it, depending on your environment, and the OP
> asked for a "standard POSIX shell idiom".

Quoting POSIX.2-1992, 3.14.13:

The condition can be EXIT; 0 (equivalent to EXIT); or a signal
specified using a symbolic name, without the SIG prefix, as listed
in Required Signals and Job Control Signals (Table 3-1 and Table 3-2
in POSIX.1 {8}). (For example: HUP, INT, QUIT, TERM). Setting a
trap for SIGKILL or SIGSTOP produces undefined results.

It has hardly changed in the current standard (POSIX.1-2017):

The condition can be EXIT, 0 (equivalent to EXIT), or a signal
specified using a symbolic name, without the SIG prefix, as listed
in the tables of signal names in the <signal.h> header defined in
XBD Chapter 13; for example, HUP, INT, QUIT, TERM. Implementations
may permit names with the SIG prefix or ignore case in signal
names as an extension. Setting a trap for SIGKILL or SIGSTOP
produces undefined results.

--
Geoff Clare <netnews@gclare.org.uk>

Re: Cleaning up background processes

<v1l5k7$1c28l$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8111&group=comp.unix.shell#8111

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_papanagnou+ng@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Fri, 10 May 2024 14:57:41 +0200
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <v1l5k7$1c28l$1@dont-email.me>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
<20240505214609.114@kylheku.com> <v1ah87$l0a8$1@news.xmission.com>
<v1gpdq$3me9$1@dont-email.me> <v1h0hk$7rkr$1@dont-email.me>
<ptg1hk-7n3.ln1@ID-313840.user.individual.net>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 10 May 2024 14:57:43 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="8e215132bebbb178b4a51becbbd1e44b";
logging-data="1444117"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Nr4RBZLgFCeB6jhhYXxRv"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:VG+wJCffIG9fYMdK/Nz1h6VSykc=
X-Enigmail-Draft-Status: N1110
In-Reply-To: <ptg1hk-7n3.ln1@ID-313840.user.individual.net>
 by: Janis Papanagnou - Fri, 10 May 2024 12:57 UTC

On 10.05.2024 14:35, Geoff Clare wrote:
> Janis Papanagnou wrote:
>
>> But the pseudo-signal 'EXIT' is non-standard (to my knowledge), so you
>> cannot generally rely on it, depending on your environment, and the OP
>> asked for a "standard POSIX shell idiom".
>
> Quoting POSIX.2-1992, 3.14.13:
>
> The condition can be EXIT; 0 (equivalent to EXIT); or a signal
> specified using a symbolic name, without the SIG prefix, as listed
> in Required Signals and Job Control Signals (Table 3-1 and Table 3-2
> in POSIX.1 {8}). (For example: HUP, INT, QUIT, TERM). Setting a
> trap for SIGKILL or SIGSTOP produces undefined results.
>
> It has hardly changed in the current standard (POSIX.1-2017):
>
> The condition can be EXIT, 0 (equivalent to EXIT), or a signal
> specified using a symbolic name, without the SIG prefix, as listed
> in the tables of signal names in the <signal.h> header defined in
> XBD Chapter 13; for example, HUP, INT, QUIT, TERM. Implementations
> may permit names with the SIG prefix or ignore case in signal
> names as an extension. Setting a trap for SIGKILL or SIGSTOP
> produces undefined results.
>

That's good. (And I was obviously misremembering.) Thanks!

BTW, I'm astonished about the "undefined results" for KILL/STOP. Yes,
on OS-level they cannot be caught but why undefined behavior on shell
level; what is the reason or practical rationale for that?

Janis

Re: Cleaning up background processes

<slrnv3v08i.17e3.naddy@lorvorc.mips.inka.de>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8112&group=comp.unix.shell#8112

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.nk.ca!2.eu.feeder.erje.net!3.eu.feeder.erje.net!feeder.erje.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: naddy@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Sat, 11 May 2024 14:30:42 -0000 (UTC)
Message-ID: <slrnv3v08i.17e3.naddy@lorvorc.mips.inka.de>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
<20240505214609.114@kylheku.com> <v1ah87$l0a8$1@news.xmission.com>
Injection-Date: Sat, 11 May 2024 14:30:42 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="41220"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Sat, 11 May 2024 14:30 UTC

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 noninteractive shell is already wrong,
although, to my surprise, it works as intended in common shells.

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.

After remembering process groups, I thought I wouldn't need to trap
any signal at all, because common interruptions (hangup, intr, quit)
send signals to the whole group. Then I discovered that FreeBSD
sh and bash start background processes with SIGINT ignored. Sigh.
So I need to trap INT and convert it to some other signal.

Another complication is that the script is typically invoked from
make(1), so we have a make(1) process, an sh(1) process executing
the script, potential children, and the background process it spawned,
all in the same process group. That means that the approach "trap
INT and manually signal process group" also signals make(1) with
whatever signal handling that carries. And I haven't checked GNU
make yet.

I also had forgotten about "kill -<sig> 0" and was playing around
with "kill -<sig> -$$", which is wrong when make(1) is the process
group leader rather than sh(1).

--
Christian "naddy" Weisgerber naddy@mips.inka.de

Re: Cleaning up background processes

<slrnv3v0m5.17e3.naddy@lorvorc.mips.inka.de>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8113&group=comp.unix.shell#8113

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.nk.ca!weretis.net!feeder9.news.weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: naddy@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Sat, 11 May 2024 14:37:57 -0000 (UTC)
Message-ID: <slrnv3v0m5.17e3.naddy@lorvorc.mips.inka.de>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
<20240505214609.114@kylheku.com> <v1ah87$l0a8$1@news.xmission.com>
<v1gpdq$3me9$1@dont-email.me>
Injection-Date: Sat, 11 May 2024 14:37:57 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="41220"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Sat, 11 May 2024 14:37 UTC

On 2024-05-08, vallor <vallor@cultnix.org> wrote:

> I'm sorry, but I've waited and nobody said anything, so
> I have to ask: Why couldn't you trap "kill -1 0" INT?

There is the cosmetic problem that you'll get termination notices
referring to the new signal ("Hangup"), so I'll go with the less
confusing

trap "kill -TERM 0" INT

but yes, it may be as simple as that. Let me run some tests...

--
Christian "naddy" Weisgerber naddy@mips.inka.de

Re: Cleaning up background processes

<v1ogll$rrf5$1@news.xmission.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8114&group=comp.unix.shell#8114

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder9.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Sat, 11 May 2024 19:24:37 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <v1ogll$rrf5$1@news.xmission.com>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de> <v1ah87$l0a8$1@news.xmission.com> <v1gpdq$3me9$1@dont-email.me> <slrnv3v0m5.17e3.naddy@lorvorc.mips.inka.de>
Injection-Date: Sat, 11 May 2024 19:24:37 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="912869"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 11 May 2024 19:24 UTC

In article <slrnv3v0m5.17e3.naddy@lorvorc.mips.inka.de>,
Christian Weisgerber <naddy@mips.inka.de> wrote:
>On 2024-05-08, vallor <vallor@cultnix.org> wrote:
>
>> I'm sorry, but I've waited and nobody said anything, so
>> I have to ask: Why couldn't you trap "kill -1 0" INT?
>
>There is the cosmetic problem that you'll get termination notices
>referring to the new signal ("Hangup"), so I'll go with the less
>confusing
>
> trap "kill -TERM 0" INT
>
>but yes, it may be as simple as that. Let me run some tests...

I don't get it. Is there any significant difference between hitting it with
TERM vs. HUP? I think either/both will generate the message to which you
allude.

Anyway, to answer the "Can't you just...?" question posed earlier, the
answer is there really is no general answer - one that will work in all
cases, all the time. Answering the overall thread question is also both
shell- and OS- specific.

Personally, I'd be happy to have a good, solid solution that was both Linux-
and bash-specific, but it sounds like OP is (unfortunately) running some
version of BSD.

I will expand on this further in my next post.

--

"This ain't my first time at the rodeo"

is a line from the movie, Mommie Dearest, said by Joan Crawford at a board meeting.

Re: Cleaning up background processes

<v1ohcd$rrf5$2@news.xmission.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8115&group=comp.unix.shell#8115

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder9.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Sat, 11 May 2024 19:36:45 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <v1ohcd$rrf5$2@news.xmission.com>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de> <20240505214609.114@kylheku.com> <v1ah87$l0a8$1@news.xmission.com> <slrnv3v08i.17e3.naddy@lorvorc.mips.inka.de>
Injection-Date: Sat, 11 May 2024 19:36:45 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="912869"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 11 May 2024 19:36 UTC

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.

Re: Cleaning up background processes

<slrnv3vr2g.1gfp.naddy@lorvorc.mips.inka.de>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8117&group=comp.unix.shell#8117

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: naddy@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Sat, 11 May 2024 22:08:16 -0000 (UTC)
Message-ID: <slrnv3vr2g.1gfp.naddy@lorvorc.mips.inka.de>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
<v1ah87$l0a8$1@news.xmission.com> <v1gpdq$3me9$1@dont-email.me>
<slrnv3v0m5.17e3.naddy@lorvorc.mips.inka.de>
<v1ogll$rrf5$1@news.xmission.com>
Injection-Date: Sat, 11 May 2024 22:08:16 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="50561"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Sat, 11 May 2024 22:08 UTC

On 2024-05-11, Kenny McCormack <gazelle@shell.xmission.com> wrote:

>>> I have to ask: Why couldn't you trap "kill -1 0" INT?
>>
>> trap "kill -TERM 0" INT
>
> I don't get it. Is there any significant difference between hitting it with
> TERM vs. HUP?

I find "Terminated" less confusing than "Hangup", that's all.
Of course it would be even better if I could keep the asynchronous
process from ignoring SIGINT in the first place... Oh, maybe I can!

Instead of

foo &

I can run

(trap - INT; exec foo) &

and indeed that seems to restore the default behavior, i.e., terminate
the process, for both FreeBSD sh and bash. Anybody see any problem
with that approach?

I'd also be interested in historical insights how this "ignore SIGINT
for asynchronous processes" behavior came to be.

--
Christian "naddy" Weisgerber naddy@mips.inka.de

Re: Cleaning up background processes

<0bg9hk-j2r.ln1@ID-313840.user.individual.net>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8132&group=comp.unix.shell#8132

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: geoff@clare.See-My-Signature.invalid (Geoff Clare)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Mon, 13 May 2024 14:14:40 +0100
Lines: 43
Message-ID: <0bg9hk-j2r.ln1@ID-313840.user.individual.net>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
<v1ah87$l0a8$1@news.xmission.com> <v1gpdq$3me9$1@dont-email.me>
<slrnv3v0m5.17e3.naddy@lorvorc.mips.inka.de>
<v1ogll$rrf5$1@news.xmission.com>
<slrnv3vr2g.1gfp.naddy@lorvorc.mips.inka.de>
Reply-To: netnews@gclare.org.uk
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: individual.net ZpeUDm2L9ABk0ZwHAANqdgAC8bsnFTXHhHhL6ei0NKiKprhzLp
X-Orig-Path: ID-313840.user.individual.net!not-for-mail
Cancel-Lock: sha1:0rlMy515y1RcADg9SFdxXu3PUtc= sha256:KJj3iUcwcv2gN4H0uBXLV9PJ0p9ag4sdsHhwfR4iWhQ=
User-Agent: Pan/0.154 (Izium; 517acf4)
 by: Geoff Clare - Mon, 13 May 2024 13:14 UTC

Christian Weisgerber wrote:

> Instead of
>
> foo &
>
> I can run
>
> (trap - INT; exec foo) &
>
> and indeed that seems to restore the default behavior, i.e., terminate
> the process, for both FreeBSD sh and bash. Anybody see any problem
> with that approach?

It doesn't/didn't work in some shells, according the POSIX rationale
(XRAT C.2.11 "Signals and Error Handling"):

Historically, some shell implementations silently ignored attempts
to use trap to set SIGINT or SIGQUIT to the default action or to
set a trap for them after they have been set to be ignored by the
shell when it executes an asynchronous subshell (and job control
is disabled). This behavior is not conforming. For example, if a
shell script containing the following line is run in the
foreground at a terminal:

(trap - INT; exec sleep 10) & wait

and is then terminated by typing the interrupt character, this
standard requires that the sleep command is terminated by the
SIGINT signal.

I don't know which shells were affected or whether any might still
be in use (without the behaviour having been corrected).

> I'd also be interested in historical insights how this "ignore SIGINT
> for asynchronous processes" behavior came to be.

That's how shells behaved before job control was invented. It was
how you could interrupt a foreground process without the signal
affecting background processes.

--
Geoff Clare <netnews@gclare.org.uk>

Re: Cleaning up background processes

<lav2ihFjo73U1@mid.individual.net>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=8135&group=comp.unix.shell#8135

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: martijn@inlv.demon.nl (Martijn Dekker)
Newsgroups: comp.unix.shell
Subject: Re: Cleaning up background processes
Date: Sun, 19 May 2024 20:34:41 +0100
Lines: 15
Message-ID: <lav2ihFjo73U1@mid.individual.net>
References: <slrnv3fm5e.jrj.naddy@lorvorc.mips.inka.de>
<20240505214609.114@kylheku.com> <v1ah87$l0a8$1@news.xmission.com>
<v1gpdq$3me9$1@dont-email.me> <v1h0hk$7rkr$1@dont-email.me>
<ptg1hk-7n3.ln1@ID-313840.user.individual.net> <v1l5k7$1c28l$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net ABZJ8202ZOkKopcE3pR39AA4zJixGPP1h0VbgSlkJ1mE5SguQ=
Cancel-Lock: sha1:E4bQ+Cj9M7HMSM/gTZ4GgeKuh/8= sha256:q8AQOr1jYjXLj3eT7zzmnfsfPmGTXTzkVLUCpUT+sk8=
User-Agent: Mozilla Thunderbird
Content-Language: en-GB
In-Reply-To: <v1l5k7$1c28l$1@dont-email.me>
 by: Martijn Dekker - Sun, 19 May 2024 19:34 UTC

Op 10-05-2024 om 13:57 schreef Janis Papanagnou:
> BTW, I'm astonished about the "undefined results" for KILL/STOP. Yes,
> on OS-level they cannot be caught but why undefined behavior on shell
> level; what is the reason or practical rationale for that?

I suspect it was to allow the shell to error out on an attempt to trap those
signals. As far as I know, yash is the only shell that actually does that.

--
|| modernish -- harness the shell
|| https://github.com/modernish/modernish
||
|| KornShell lives!
|| https://github.com/ksh93/ksh


devel / comp.unix.shell / Cleaning up background processes

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor