How to kill all processes that were opened by a shell script upon ctrl + C?

前端 未结 1 2039
星月不相逢
星月不相逢 2021-02-20 03:47

I\'ve got a couple python scripts which I start collectively from a shell script as follows:

#!/bin/bash
python prog1.py &
python prog2.py &
python prog3         


        
1条回答
  •  佛祖请我去吃肉
    2021-02-20 04:32

    Let the bash script catch SIGINT, and have it kill everything in the current process group:

    intexit() {
        # Kill all subprocesses (all processes in the current process group)
        kill -HUP -$$
    }
    
    hupexit() {
        # HUP'd (probably by intexit)
        echo
        echo "Interrupted"
        exit
    }
    
    trap hupexit HUP
    trap intexit INT
    
    python prog1.py &
    python prog2.py &
    python prog3.py &
    
    wait
    

    0 讨论(0)
提交回复
热议问题