I'm aware that similar questions have been asked before on SO (for example here) but I can't get it to work for my case.
I have a bash script called kubetail which evaluates a set of background commands started from the script like this:
CMD="cat <( eval "${command_to_tail}" )"
eval "$CMD"
where command_to_tail
calls several subprocesses (kubectl) and aggregates their output into one stream. The problem is that when ctrl+c
is pressed during eval
it won't interrupt the subprocesses when the main script stops. For example this is shown when I run ps -Af | grep kubectl
after I've interrupted the script (the kubectl
is spawned by my script):
$ ps -Af | grep kubectl
501 85748 85742 0 9:48AM ttys014 0:00.16 kubectl --context= logs pod-4074277481-3tlx6 core -f --since=10s --namespace=
501 85750 85742 0 9:48AM ttys014 0:00.17 kubectl --context= logs pod-4074277481-9r224 core -f --since=10s --namespace=
501 85752 85742 0 9:48AM ttys014 0:00.16 kubectl --context= logs pod-4074277481-hh9bz core -f --since=10s --namespace=
I've tried various forms of trap - INT
but I fail to find a solution that kills all subprocesses on ctrl+c
. Any suggestions?