trap訊號捕捉詳解

來源:互聯網
上載者:User

標籤:trap捕捉訊號

<--目錄-->

1)trap介紹

2)trap訊號列表

3)trap使用例子

4)測試INT(2)訊號

5)同時測試多個訊號

6)shell跳板機案例


【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)訊號】

[[email protected]lhost ~]# trap "" 2     <== 屏蔽Ctrl+C訊號,""引號裡面為空白,就是說你按了Crtl+C後,不做任何動作,相當於屏蔽了Ctrl+C

[[email protected] ~]# 此時無法執行Ctrl+C

[[email protected] ~]# trap ":" 2    <== 恢複ctrl+c訊號

[[email protected] ~]# ^C            <== 此時可以執行Crtl+C了

[[email protected] ~]# trap "echo -n ‘you are typing ctrl+c‘" 2  <== 按了Ctrl+C後接受到2的訊號就會輸出雙引號裡面的內容

[[email protected] ~]# ^Cyou are typing ctrl+c

[[email protected] ~]# ^Cyou are typing ctrl+c

[[email protected] ~]# ^Cyou are typing ctrl+c

[[email protected] ~]# trap ":" 2    <== 再還原本樣Ctrl+C訊號

[[email protected] ~]# ^C            <== 此時再按Crtl+C變成原始的輸出



【同時測試多個訊號】

[[email protected] ~]# trap "" HUP INT QUIT TSTP TERM

[[email protected] ~]# trap ":" HUP INT QUIT TSTP TERM

[[email protected] ~]# trap "" 1 2 3 20 15

[[email protected] ~]# trap ":" 1 2 3 20 15



【shell跳板機案例】(觸發訊號後屏蔽訊號)

 方法:

 1)首先做好ssh+key認證

 2)實現傳統的遠端連線菜單選擇指令碼

 3)利用Linux訊號防止使用者在跳板機上操作

 4)使用者登陸後即呼叫指令碼

 [[email protected] ~]# cat /etc/profile.d/tiaoban.sh 

 [ $UID -ne 0 ] && . /server/scripts/tiaoban.sh

 [[email protected] ~]#  cat /server/scripts/tiaoban.sh

 function trapper () {

 trap ‘:‘ INT EXIT TSTP TEMP HUP

 }


 while :

 do 

         trapper       #我在這裡調用函數trapper,然則調用了上面五個訊號,其有中Ctrl+c訊號

         clear         #清屏

         cat <<menu    #終端列印出下面三排菜單供普通使用者選擇

             1)web a

             2)web b

             3)exit

 menu


         read -p "please select:" num    #讓使用者選擇一個數字

         case "$num" in

         1)

         echo 1

         ssh 192.168.1.107

         ;;

         2)

         echo 2

         ssh 192.168.1.108

         ;;

         3|*)

         exit

         esac

 done


 普通使用者測試登陸:

 650) this.width=650;" src="http://s5.51cto.com/wyfs02/M02/82/EB/wKiom1dlAQjzY7wDAAB2roUmxwg461.png-wh_500x0-wm_3-wmp_4-s_575981912.png" title="QQ圖片20160618160719.png" alt="wKiom1dlAQjzY7wDAAB2roUmxwg461.png-wh_50" />

 輸入普通使用者名稱和密碼

 650) this.width=650;" src="http://s2.51cto.com/wyfs02/M00/82/EA/wKioL1dlAUfyr8VrAABPM3KvaT8814.png-wh_500x0-wm_3-wmp_4-s_2160818615.png" title="QQ圖片20160618160719.png" alt="wKioL1dlAUfyr8VrAABPM3KvaT8814.png-wh_50" />

 在這裡按Ctrl+c沒效果,你必須選擇敲一個數字串連,或者選擇敲3退出

 650) this.width=650;" src="http://s4.51cto.com/wyfs02/M01/82/EB/wKiom1dlAyixA6pIAAAYXRilLsQ103.png" title="QQ圖片20160618160719.png" alt="wKiom1dlAyixA6pIAAAYXRilLsQ103.png" />

 


本文出自 “wsyht的部落格” 部落格,請務必保留此出處http://wsyht2015.blog.51cto.com/9014030/1790623

trap訊號捕捉詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.