trap信號捕捉命令介紹與shell結合實戰講解

<--目錄-->

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]#


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章