如果[關閉],我怎麼能退出整個腳本

如果[關閉],我怎麼能退出整個腳本

如果腳本已使用 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 命令
發送到進程的信號列表

相關內容