开发者

catching keyboard interrupt

I have a bash script that is executing a program in a loop and reading the output from the program. I wish that when I hit control-c, it terminates the program as well as the script.

I tried this but does not seem to terminate the program.

control_c() {
   exit
}

while true ; do 

    trap control_c SIGI开发者_运维百科NT

    my_command | while read line ; do
       echo $line 
       ...
    done
done

Can someone show me the correct way to accomplish what I have described? Thank you!


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


Try killing the program in your control_c() function, e.g.,

pkill my_command
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜