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