![如果[關閉],我怎麼能退出整個腳本](https://rvso.com/image/1024440/%E5%A6%82%E6%9E%9C%5B%E9%97%9C%E9%96%89%5D%EF%BC%8C%E6%88%91%E6%80%8E%E9%BA%BC%E8%83%BD%E9%80%80%E5%87%BA%E6%95%B4%E5%80%8B%E8%85%B3%E6%9C%AC.png)
如果腳本已使用 bash test.sh 或 ./test.sh 執行,如何停止該腳本?
# if the script was run with bash test.sh or ./test.sh, stop
echo "From here the script only works if it has been run with source test.sh or . test.sh"
答案1
如果不是在背景發送,請按 ctrl+c。如果它在後台,則運行
ps aux | grep test.sh
然後從 PID 欄位中取得進程 ID,
然後使用 Kill 來停止它。例如,如果 PID 為 3946。運行
kill 3946
,然後再次運行
ps aux | grep test.sh
如果您得到的唯一結果是 grep 那麼該程序已停止。否則你可能需要使用
kill -9 3946來強制它
,這有點殘酷,但作為最後的手段。
您可以執行
kill -l ,這將為您提供可以使用kill 命令
發送到進程的信號列表