kol*_*pto 22 bash shell-scripting
如何在 bash 脚本中实现ctrl+c处理,从而中断脚本,以及脚本启动的当前正在运行的命令?
(想象一下,有一个脚本可以执行一些长时间运行的命令。用户点击ctrl+c并中断命令,但脚本继续运行。)我需要它的行为方式使他们都被杀死。
Kvi*_*sle 26
为此,您可以创建一个在收到 SIGINT 时要调用的子例程,并且您需要运行trap 'subroutinename' INT.
例子:
#!/bin/bash
int_handler()
{
echo "Interrupted."
# Kill the parent process of the script.
kill $PPID
exit 1
}
trap 'int_handler' INT
while true; do
sleep 1
echo "I'm still alive!"
done
# We never reach this part.
exit 0
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10054 次 |
| 最近记录: |