<--目录-->

1)trap介绍

2)trap信号列表

3)trap使用例子

4)测试INT(2)信号

5)同时测试多个信号

6)trap结合shell实战

7)执行脚本测试



【trap介绍】

通过trap命令用于指定在接收到信号后要采取的行动;trap命令的一种常见用途是在脚本程序被中断时完成清理工作,历吏上,shell总是用数字来代表信号


【trap信号列表】

下面列出一些比较重要的信号(括号里面的数字是传统的信号编号)

信号          说明 

HUP(1)      挂起,通常因终端掉线或用户退出而引发 

INT(2)      中断,通常因按下Crtl+C组合健而引发                 

QUIT(3)     退出,通常因某些严重的执行错误而引发         

ABRT(6)     中止,通常因某些严重的执行错误而引发         

ALRM(14)    报警,通常用来处理超时 |

TERM(15)    终止,通常在系统关机时发送 

TSTP(20)    停止进程的运行,但该信号可以被处理和忽略,用户健入SUSP字符时(通常是Ctrl-Z)发出这个信号



【trap使用例子】

通常我们需要忽略的信号有HUP INT QUIT TSTP TERM,也就是信号1,2,3,20,15使用下面的语句可以使这些中断信号被忽略

trap命令的参数分为两部分,前一部分是接收到指定信号时将要采取的行动,后一部分是要处理的信号名,如下例子:

########################

# trap command signal  #

########################

signal是指收到的信号,command是指接收该信号采取的行动,如下为两种简单的信号

######################################################################################################

# trap "command(要执行的动作命令)" 1 2 3 20 15 或 trap "command(要执行的动作命令)" HUP INT QUIT TSTP #

######################################################################################################



【测试INT(2)信号】

[root@localhost ~]# trap "" 2     <== 屏蔽Ctrl+C信号,按Crtl+C健,终端无反应
[root@localhost ~]# 此时无法执行Ctrl+C
[root@localhost ~]# trap ":" 2    <== 恢复ctrl+c信号
[root@localhost ~]# ^C            <== 此时可以执行Crtl+C了
[root@localhost ~]# trap "echo -n 'you are typing ctrl+c'" 2  <== 接收到信号2会输出引号内容
[root@localhost ~]# ^Cyou are typing ctrl+c
[root@localhost ~]# ^Cyou are typing ctrl+c
[root@localhost ~]# ^Cyou are typing ctrl+c
[root@localhost ~]# trap ":" 2    <== 再还原本样Ctrl+C信号
[root@localhost ~]# ^C            <== 此时再按Crtl+C变成原始的输出




【同时测试多个信号】

[root@localhost ~]# trap "" HUP INT QUIT TSTP TERM
[root@localhost ~]# trap ":" HUP INT QUIT TSTP TERM
[root@localhost ~]# trap "" 1 2 3 20 15
[root@localhost ~]# trap ":" 1 2 3 20 15


 


【trap结合shell实战】

#!/bin/bash
function xh {             #循环函数
    for i in {1..100}
    do
        echo $i
        sleep 1
    done
    }
function xinhao {         #捕捉信号函数
    trap 'tiwen' 1 2 3 20 15      
}
function tiwen {          #供用户选择是或否的菜单
    cat << menu
        yes) 退出
        no)  继续
menu
read -p "print input yes or no,Franks!!!" input     #让用户输入选项
        case $input in
                yes)
                  exit
                  ;;
                no)
                  echo “程序继续执行”
                 ;;
        esac
}
function index {         #让用户选择运行或退出脚本的函数
    cat <<menu
        1) 循环1到100
        2) 退出
menu
} 
clear  #清屏
index  #运行脚本函数
read -p "print select a num:"  num
case $num in   #用户选择执行的分支
1)
  xinhao
  xh
  ;;
2)
  exit
  ;;
esac


执行脚本测试】 

#执行示例1: 

Ctrl+c信号展示

[root@localhost opt]# sh a.sh
        1) 循环1到100
        2) 退出
print select a num:1
1
2
^C      yes) 退出
        no)  继续
print input yes or no,Franks!!!no
“程序继续执行”
3
4
5
^C      yes) 退出
        no)  继续
print input yes or no,Franks!!!yes
[root@localhost opt]#


#执行示例2

kill信号展示,开两个终端

终端1操作:

[root@localhost opt]# sh a.sh
        1) 循环1到100
        2) 退出
print select a num:1
1
2
3


终端2操作:

[root@localhost ~]# ps aux | grep a.sh
root     27608  0.0  0.0 106064  1356 pts/1    S+   22:58   0:00 sh a.sh
root     27614  0.0  0.0 103244   892 pts/4    S+   22:58   0:00 grep a.sh
[root@localhost ~]# kill 27608


终端1看到又提问我要不要继续还是退出,kill信号捕捉成功

        1) 循环1到100
        2) 退出
print select a num:1
1
2
3
4
5
6
7
8
9
10
11
        yes) 退出
        no)  继续
print input yes or no,Franks!!!yes
[root@localhost opt]#