Keyboard interruption

I have a bash script that runs a program in a loop and reads the output from a program. I want that when I hit control-c, it exits the program as well as the script.

I tried this but didn't seem to end the program.

control_c() { exit } while true ; do trap control_c SIGINT my_command | while read line ; do echo $line ... done done 

Can someone show me the correct way to accomplish what I described? Thanks!

+6
source share
2 answers

You can do something like this:

 control_c() { kill $PID exit } trap control_c SIGINT while true ; do my_command | while read line ; do PID=$! echo $line ... done 
+4
source

Try to kill the program in your control_c() function, for example,

 pkill my_command 
+2
source

Source: https://habr.com/ru/post/886827/


All Articles