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

Want to kill the script B, which is initially started from script A, when using CTRL + C to kill script A.

50 views
Skip to first unread message

Hongyi Zhao

unread,
Feb 18, 2016, 7:38:24 PM2/18/16
to
Hi all,

I have two shell bash scripts, say, A and B.

The script A is as follows:

---------&<---------
#!/bin/bash
# Here is the content of script-A

bla bla

./script-B &

bla bla
---------&<---------

When script A is running, I using CTRL + C to kill script A. But I find
that if the script B is running when I issue ``CTRL + C'', it will still
leave in the running status after the script A is killed.

So, is it possible to ensure the script B also be killed when using CTRL
+ C to kill the script A.

Regards
--
.: Hongyi Zhao [ hongyi.zhao AT gmail.com ] Free as in Freedom :.

Aragorn

unread,
Feb 18, 2016, 9:05:46 PM2/18/16
to
On Friday 19 Feb 2016 01:35, Hongyi Zhao conveyed the following to
comp.unix.shell...

> Hi all,
>
> I have two shell bash scripts, say, A and B.
>
> The script A is as follows:
>
> ---------&<---------
> #!/bin/bash
> # Here is the content of script-A
>
> bla bla
>
> ./script-B &
>
> bla bla
> ---------&<---------
>
> When script A is running, I using CTRL + C to kill script A. But I
> find that if the script B is running when I issue ``CTRL + C'', it
> will still leave in the running status after the script A is killed.
>
> So, is it possible to ensure the script B also be killed when using
> CTRL + C to kill the script A.

You are starting script B in a subshell, and you are daemonizing it ─
i.e. you are shoving it into the background ─ so the answer to your
question is "No", or at least, "Not as you would want it." Ctrl+C only
terminates the active process in the current shell. It doesn't touch
anything running in the background.

Is there a reason why you want it to run in the background ─ e.g. you
want the variables of script B to exist in their own address space? If
not, then instead of invoking it via a subshell, you could source it
into the running script, i.e.

. ./script

... or...

source ./script

If you do need script B to run as a separate process ─ i.e. in the
background, or simply in a separate address space ─ then you're going to
have to create a PID file for it, and then parse it, kill the process
and clean up the PID file.

Also note that relying on Ctrl+C to terminate a script is bad coding
practice. You could insert far more elegant ways of terminating the
script.

--
= Aragorn =

http://www.linuxcounter.net - registrant #223157

Icarus Sparry

unread,
Feb 18, 2016, 9:36:18 PM2/18/16
to
On Fri, 19 Feb 2016 00:35:24 +0000, Hongyi Zhao wrote:

> Hi all,
>
> I have two shell bash scripts, say, A and B.
>
> The script A is as follows:
>
> ---------&<---------
> #!/bin/bash # Here is the content of script-A
>
> bla bla
>
> ./script-B &
>
> bla bla ---------&<---------
>
> When script A is running, I using CTRL + C to kill script A. But I find
> that if the script B is running when I issue ``CTRL + C'', it will still
> leave in the running status after the script A is killed.
>
> So, is it possible to ensure the script B also be killed when using CTRL
> + C to kill the script A.
>
> Regards

You can capture the process id of script-B, using

./script-B &
pid=$!

and then you can arrange to trap the control-C and kill the script-B

trap "kill -2 $pid" 2

As always reading the manual helps.

Hongyi Zhao

unread,
Feb 18, 2016, 11:43:41 PM2/18/16
to
On Fri, 19 Feb 2016 02:36:13 +0000, Icarus Sparry wrote:

> You can capture the process id of script-B, using
>
> ./script-B &
> pid=$!
>
> and then you can arrange to trap the control-C and kill the script-B
>
> trap "kill -2 $pid" 2

Tried this in script-A but failed:

------- script-A ------
#!/bin/bash

./script-B &
pid=$!

bla

trap "kill -2 $pid" 2
-----------------

Then using control-C to kill script-A, and the script-B still be running.

Regards
>
> As always reading the manual helps.

Rakesh Sharma

unread,
Feb 19, 2016, 1:00:26 AM2/19/16
to
On Friday, 19 February 2016 10:13:41 UTC+5:30, Hongyi Zhao wrote:

>
> ------- script-A ------
> #!/bin/bash
>
> ./script-B &
> pid=$!
>
> bla
>
> trap "kill -2 $pid" 2
> -----------------
>
> Then using control-C to kill script-A, and the script-B still be running.
>

You are not able to slay the script-B since there is no interrupt catching mechanism in place there. For unconditional killings, you have to bring in
the heavy artillery, (kill -9).

So either do this in case you are unwilling to change script-b:
trap 'kill -9 "$pid"' 2
or better yet, you could put in place an interrup-catching mechanism in script-b
by placing this line in there:

trap 'echo "Caught SIGINT, exiting...";exit 1' 2
and then you could retain the line
trap 'kill -2 "$pid"' 2 in script-A

Hongyi Zhao

unread,
Feb 19, 2016, 1:41:50 AM2/19/16
to
On Thu, 18 Feb 2016 22:00:11 -0800, Rakesh Sharma wrote:

> You are not able to slay the script-B since there is no interrupt
> catching mechanism in place there. For unconditional killings, you have
> to bring in the heavy artillery, (kill -9).

Really? But I've found the following method will also do the job:

script-A:
---
#!/bin/bash

control_c()
# run if user hits control-c
{
echo -en "\n*** Ouch! Exiting ***\n"
if [ "x"$pid != "x" ]; then
kill $pid
fi
exit $?
}

# trap keyboard interrupt (control-c) and call control_c()
# trap control_c SIGINT
#or
trap control_c INT

bla


./script-B &
pid=$!
---

>
> So either do this in case you are unwilling to change script-b:
> trap 'kill -9 "$pid"' 2
> or better yet, you could put in place an interrup-catching mechanism in
> script-b by placing this line in there:
>
> trap 'echo "Caught SIGINT, exiting...";exit 1' 2
> and then you could retain the line
> trap 'kill -2 "$pid"' 2 in script-A

Thanks a lot for your above methods.

Regards

Hongyi Zhao

unread,
Feb 19, 2016, 2:00:48 AM2/19/16
to
On Fri, 19 Feb 2016 06:38:49 +0000, Hongyi Zhao wrote:

> if [ "x"$pid != "x" ]; then

More elegant way:

if [ "x"$pid != "x" ] && kill -0 $pid ; then

Hongyi Zhao

unread,
Feb 19, 2016, 3:00:41 AM2/19/16
to
On Thu, 18 Feb 2016 22:00:11 -0800, Rakesh Sharma wrote:
[snip]
> So either do this in case you are unwilling to change script-b:
> trap 'kill -9 "$pid"' 2

I want to the script A exit too. So the above code should be changed as
follows:

trap 'kill -9 "$pid" ; exit ' 2

> or better yet, you could put in place an interrup-catching mechanism in
> script-b by placing this line in there:
>
> trap 'echo "Caught SIGINT, exiting...";exit 1' 2
> and then you could retain the line
> trap 'kill -2 "$pid"' 2 in script-A

Also, this should be changed into the following for my case:

trap 'kill -2 "$pid" ; exit ' 2

Regards

Hongyi Zhao

unread,
Feb 19, 2016, 3:02:01 AM2/19/16
to
On Fri, 19 Feb 2016 06:57:47 +0000, Hongyi Zhao wrote:

>> if [ "x"$pid != "x" ]; then
>
> More elegant way:
>
> if [ "x"$pid != "x" ] && kill -0 $pid ; then

Using the following more elegant way:

if [ "x"$pid != "x" ] && kill -0 $pid &> /dev/null ; then

Michael Paoli

unread,
Sep 7, 2016, 7:46:03 AM9/7/16
to
$ cat A
#!/bin/sh
./B &
B_PID="$!"
trap "kill -9 $B_PID $$" 2
wait
$ cat B
#!/bin/sh
while :
do
sleep 1
done
$ ./A &
[1] 10180
$ ps
PID TTY TIME CMD
10180 pts/19 00:00:00 A
10181 pts/19 00:00:00 B
10222 pts/19 00:00:00 sleep
10223 pts/19 00:00:00 ps
28249 pts/19 00:00:00 bash
$ kill -2 10180
$
[1]+ Killed ./A
$ ps
PID TTY TIME CMD
10275 pts/19 00:00:00 ps
28249 pts/19 00:00:00 bash
$
0 new messages