![如果[关闭],我该如何退出整个脚本](https://linux22.com/image/1024440/%E5%A6%82%E6%9E%9C%5B%E5%85%B3%E9%97%AD%5D%EF%BC%8C%E6%88%91%E8%AF%A5%E5%A6%82%E4%BD%95%E9%80%80%E5%87%BA%E6%95%B4%E4%B8%AA%E8%84%9A%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 命令
发送给进程的信号列表。