How to kill a background process created in a script

前端 未结 2 907
遥遥无期
遥遥无期 2021-01-15 14:00

Suppose I input the following in a shell

(while true; do echo hahaha; sleep 1; done)&

Then I know I can kill it by

fg;          


        
2条回答
  •  半阙折子戏
    2021-01-15 14:36

    (while true; do echo hahaha; sleep 1; done)&
    RUNNING_PID=$!
    kill ${RUNNING_PID}
    

    $! will pick up the PID of the process that is running so you can do with it as you wish

提交回复
热议问题