1

I am executing a bash script and I want to correctly terminate a command (by pressing CTRL+C) that I am launching inside the script. This is the script:

trap ctrl_c SIGINT

function ctrl_c()
{
PID = $!
kill -s SIGINT -$PID
wait $PID
echo "mycommand stopped!"
}

mycommand directory -option | grep --line-buffered -A 'Hello|Bye' >> output

But it doesn't stop mycommand.

juvor
  • 41
  • 1
  • 6
  • So you run this in the foreground, and hit CTRL+C manually? Do you see anything from the trap function in your output file? Is the `$!` pid definitely also a process group ID? – Useless Oct 02 '20 at 14:41
  • In my output file I see only the output of mycommand. Not sure about $!, I have been told to use it to get the last process id. – juvor Oct 02 '20 at 14:44
  • `$!` will get you the PID of the last process sent to the background. `$$` will get _your_ PID. – DopeGhoti Oct 02 '20 at 14:45
  • 1
    `PID = $!` is wrong not only for the above, there can be no spaces around the equal sign! – Vlastimil Burián Oct 02 '20 at 14:58
  • Do you have a `#!` line as the first line of your script? How are you calling it? (This is important because it determines the shell that gets used.) – roaima Oct 02 '20 at 15:01
  • Side note, what is the dash (minus) sign doing in `kill -s SIGINT -$PID` in front of the `$PID`. – Vlastimil Burián Oct 02 '20 at 15:02
  • 1
    making it kill the whole process group – Useless Oct 02 '20 at 15:06
  • So I should use $$ instead of $!? – juvor Oct 02 '20 at 15:55
  • See the comments [here](https://unix.stackexchange.com/q/612380/414777). Your script won't run any traps at all until `mycommand ... |` terminates. If `mycommand` is catching or ignoring `SIGINT`, it won't terminate and there's nothing you can do about it from its parent script. And, as mentioned, `$!` is the pid of the last command run in *background* (with `&`), not in foreground. –  Oct 02 '20 at 16:08
  • Besides, your trap wouldn't have worked even if the command was started in background (`mycommand ... | .. &`), because background commands in scripts run with their `SIGINT` ignored. –  Oct 02 '20 at 16:21
  • Yes, mycommand is indeed catching SIGINT which is the correct way to terminate mycommand. Hence why I want to trap the ctrl_c event from the parent script, if I don't when I press the ctrl_c it will terminate only the parent script but mycommand is still running. – juvor Oct 02 '20 at 16:47
  • See https://stackoverflow.com/a/46061734 – Dani_l Oct 02 '20 at 17:20
  • Thank you, from the link you provided it seems that solution lies in setting the monitor. Not sure on how do it though. – juvor Oct 02 '20 at 18:55
  • Exactly, it has to do also the clean up actions and everything you said when ctrl+c event is triggered. I tested the trap function with only `echo` and it works, the other code doesn't. And of course `mycommand directory -option | grep --line-buffered -A 'Hello|Bye' >> output` works just fine. When I run `myccomand` alone (without the grep) in the terminal all I need to do is press ctrl+c to correctly stop it. Now I am struggling on how to do the same in my bash script. No, it's not pseudocode. – juvor Oct 02 '20 at 19:28

0 Answers0