Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

trap doesn't respond control-c correctly.

22 views
Skip to first unread message

hongy...@gmail.com

unread,
Dec 4, 2022, 3:47:05 AM12/4/22
to
Hi here,

On Ubuntu 22.10, I'm using the following shipped bash version:

$ bash --version
GNU bash, version 5.2.2(1)-release (x86_64-pc-linux-gnu)

In my case, I've a bash script which has the following line in it:

trap 'echo -en "\n*** Ouch! Exiting ***\n"; sudo kill -- -$(ps -o pgid= $$ | egrep -o [0-9]+); exit 1' 2


Then when the script is running, when I hit the control-c, I just noticed the following information given on the screen:

blabla (Here is the normal information given by the script when running).
^C

This issue did not occur until I upgraded to this version of the operating system. I'm very confused by this behavior.

Any comments and tips will be appreciated.

Regards,
Zhao

Kees Nuyt

unread,
Dec 4, 2022, 10:35:05 AM12/4/22
to
With GNU bash, version 5.0.3(1)-release
(arm-unknown-linux-gnueabihf) I get:

blah
blah
^C
*** Ouch! Exiting ***
Terminated
$

So you will have to read the release notes of bash between your
previous version and the current one.

--
Regards,
Kees Nuyt

Ben Bacarisse

unread,
Dec 4, 2022, 11:13:50 AM12/4/22
to
"hongy...@gmail.com" <hongy...@gmail.com> writes:

> On Ubuntu 22.10, I'm using the following shipped bash version:
>
> $ bash --version
> GNU bash, version 5.2.2(1)-release (x86_64-pc-linux-gnu)

Same here.

> In my case, I've a bash script which has the following line in it:
>
> trap 'echo -en "\n*** Ouch! Exiting ***\n"; sudo kill -- -$(ps -o
> pgid= $$ | egrep -o [0-9]+); exit 1' 2

What's all the ps and egrep stuff for? Isn't the net effect just $$?

> Then when the script is running, when I hit the control-c, I just
> noticed the following information given on the screen:
>
> blabla (Here is the normal information given by the script when
> running). ^C
>
> This issue did not occur until I upgraded to this version of the
> operating system. I'm very confused by this behavior.

Presumably the issue is that the trap does not work, but it does for
me. Is Ctrl-C sending the right signal?

Can you post a minimal example that fails for you? This example:

#!/bin/bash

trap 'echo -en "\n*** Ouch! Exiting ***\n"; exit 1' 2

while true; do
echo blah
done

works for me:

$ ./script
blah
blah
blah
blah
blah
blah
^C./script: line 6: echo: write error: Interrupted system call

*** Ouch! Exiting ***

--
Ben.

Ed Morton

unread,
Dec 4, 2022, 2:03:48 PM12/4/22
to
The most common source of confusion in this area is, I think, when
people are running a shell that calls another command in a sub-shell.
They send an interrupt and the sub-shell gets the interrupt but the
subshell traps the interrupt to handle it itself and then doesn't exit
with an interrupt exit status so the calling shell where you expect to
see an interrupt trapped doesn't fire because that shell doesn't know an
interrupt happened.

We can't help you diagnose a problem in a script given just 1 line of
the script though - create and provide a **MINIMAL** complete sample
script that has this problem and then we can help you.

Also, if you google for things like "bash interrupt subshell" you'll get
some hits with more info on this topic. Also see
https://mywiki.wooledge.org/SignalTrap.

Ed.

hongy...@gmail.com

unread,
Dec 4, 2022, 8:10:28 PM12/4/22
to
On Monday, December 5, 2022 at 12:13:50 AM UTC+8, Ben Bacarisse wrote:
> "hongy...@gmail.com" <hongy...@gmail.com> writes:
>
> > On Ubuntu 22.10, I'm using the following shipped bash version:
> >
> > $ bash --version
> > GNU bash, version 5.2.2(1)-release (x86_64-pc-linux-gnu)
> Same here.
> > In my case, I've a bash script which has the following line in it:
> >
> > trap 'echo -en "\n*** Ouch! Exiting ***\n"; sudo kill -- -$(ps -o
> > pgid= $$ | egrep -o [0-9]+); exit 1' 2
> What's all the ps and egrep stuff for? Isn't the net effect just $$?

I've a bunch of other processes spawned from this script, and I hope to kill them all, so that the orphan process will not be left behind.

> > Then when the script is running, when I hit the control-c, I just
> > noticed the following information given on the screen:
> >
> > blabla (Here is the normal information given by the script when
> > running). ^C
> >
> > This issue did not occur until I upgraded to this version of the
> > operating system. I'm very confused by this behavior.
> Presumably the issue is that the trap does not work, but it does for
> me. Is Ctrl-C sending the right signal?
>
> Can you post a minimal example that fails for you? This example:
>
> #!/bin/bash
>
> trap 'echo -en "\n*** Ouch! Exiting ***\n"; exit 1' 2
>
> while true; do
> echo blah
> done
>
> works for me:
>
> $ ./script
> blah
> blah
> blah
> blah
> blah
> blah
> ^C./script: line 6: echo: write error: Interrupted system call
>
> *** Ouch! Exiting ***

I observed the same behavior as you. But my real original script is rather complicated, and it will take some time to work out a minimal example.

> --
> Ben.

Zhao

Ben Bacarisse

unread,
Dec 4, 2022, 8:15:57 PM12/4/22
to
"hongy...@gmail.com" <hongy...@gmail.com> writes:

> On Monday, December 5, 2022 at 12:13:50 AM UTC+8, Ben Bacarisse wrote:
>> "hongy...@gmail.com" <hongy...@gmail.com> writes:
>>
>> > On Ubuntu 22.10, I'm using the following shipped bash version:
>> >
>> > $ bash --version
>> > GNU bash, version 5.2.2(1)-release (x86_64-pc-linux-gnu)
>> Same here.
>> > In my case, I've a bash script which has the following line in it:
>> >
>> > trap 'echo -en "\n*** Ouch! Exiting ***\n"; sudo kill -- -$(ps -o
>> > pgid= $$ | egrep -o [0-9]+); exit 1' 2
>> What's all the ps and egrep stuff for? Isn't the net effect just $$?
>
> I've a bunch of other processes spawned from this script, and I hope
> to kill them all, so that the orphan process will not be left behind.

Ed Morton called it! Did you read his reply?

--
Ben.

hongy...@gmail.com

unread,
Dec 4, 2022, 9:20:48 PM12/4/22
to
On Monday, December 5, 2022 at 9:15:57 AM UTC+8, Ben Bacarisse wrote:
> "hongy...@gmail.com" <hongy...@gmail.com> writes:
>
> > On Monday, December 5, 2022 at 12:13:50 AM UTC+8, Ben Bacarisse wrote:
> >> "hongy...@gmail.com" <hongy...@gmail.com> writes:
> >>
> >> > On Ubuntu 22.10, I'm using the following shipped bash version:
> >> >
> >> > $ bash --version
> >> > GNU bash, version 5.2.2(1)-release (x86_64-pc-linux-gnu)
> >> Same here.
> >> > In my case, I've a bash script which has the following line in it:
> >> >
> >> > trap 'echo -en "\n*** Ouch! Exiting ***\n"; sudo kill -- -$(ps -o
> >> > pgid= $$ | egrep -o [0-9]+); exit 1' 2
> >> What's all the ps and egrep stuff for? Isn't the net effect just $$?
> >
> > I've a bunch of other processes spawned from this script, and I hope
> > to kill them all, so that the orphan process will not be left behind.
> Ed Morton called it! Did you read his reply?

I've read Ed Morton's valuable reply and am trying to find the root of the problem based on his comments. Still, it seems like a needle-in-a-haystack problem, at least for now.

> --
> Ben.

Zhao
0 new messages