From: Russ P. on
I would like to be able to abort a bash script with control-c when I
can see that something has gone awry. However, it often doesn't work,
depending on exactly when you hit control-c, because the script has
spawned other scripts, and control-c only apparently only aborts the
one that happens to be currently executing. What do I need to put in
the original script to ensure that control-c aborts the whole
enchilada? Thanks.

Russ P.
From: Thomas 'PointedEars' Lahn on
Russ P. wrote:

> I would like to be able to abort a bash script with control-c when I
> can see that something has gone awry. However, it often doesn't work,
> depending on exactly when you hit control-c, because the script has
> spawned other scripts, and control-c only apparently only aborts the
> one that happens to be currently executing. What do I need to put in
> the original script to ensure that control-c aborts the whole
> enchilada? Thanks.

Whatever meal you are talking about, search for `trap' and `SIGINT'.


PointedEars
From: Maxwell Lol on
"Russ P." <russ.paielli(a)gmail.com> writes:

> I would like to be able to abort a bash script with control-c when I
> can see that something has gone awry. However, it often doesn't work,
> depending on exactly when you hit control-c, because the script has
> spawned other scripts, and control-c only apparently only aborts the
> one that happens to be currently executing. What do I need to put in
> the original script to ensure that control-c aborts the whole
> enchilada? Thanks.


Try something like

program1 & pids="$pids $!"
program2 & pids="$pids $!"
trap "echo caught signal 15;kill -15 $pids" 15

wait $pids

From: Russ P. on
On May 27, 4:03 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
wrote:
> Russ P. wrote:
> > I would like to be able to abort a bash script with control-c when I
> > can see that something has gone awry. However, it often doesn't work,
> > depending on exactly when you hit control-c, because the script has
> > spawned other scripts, and control-c only apparently only aborts the
> > one that happens to be currently executing. What do I need to put in
> > the original script to ensure that control-c aborts the whole
> > enchilada? Thanks.
>
> Whatever meal you are talking about, search for `trap' and `SIGINT'.
>
> PointedEars

I am aware of "trap," but I am not sure exactly what it does. Suppose
I have this near the top of my bash script:

trap 'exit' INT TERM # exit when interrupted or terminated

Is this guaranteed to cause the script and all of the subprocesses
that it has spawned to terminate when I hit control-C?

Russ P.
From: Thomas 'PointedEars' Lahn on
Russ P. wrote:

> I am aware of "trap," but I am not sure exactly what it does.

You could RTFM.

> Suppose I have this near the top of my bash script:
>
> trap 'exit' INT TERM # exit when interrupted or terminated
>
> Is this guaranteed to cause the script and all of the subprocesses
> that it has spawned to terminate when I hit control-C?

If you use the proper signal names, it is guaranteed to terminate the shell.
You must have a really bad day if you have a child process that does not
react to SIGINT; it does not need to terminate then, though, that depends on
the subprocess. You would have a hard time finding a child process that
does not terminate on SIGTERM or SIGKILL, though.

Please trim your quotes to the relevant minimum, do not quote signatures
unless you are referring to them.

--
PointedEars