1. 程式人生 > >16.2、信號捕捉

16.2、信號捕捉

col stopped stop ces 信號 width localhost pac 捕捉

1、信號幫助:

方法一:#man 7 signal

方法二:#kill -l

方法三:#trap -l


2、常用信號列表

Signal Value Action Comment

──────────────────────────────────────────────────────────────────────

SIGHUP 1 Term Hangup detected on controlling terminal

or death of controlling process

SIGINT 2 Term Interrupt from keyboard

SIGQUIT 3 Core Quit from keyboard

SIGILL 4 Core Illegal Instruction

SIGABRT 6 Core Abort signal from abort(3)

SIGFPE 8 Core Floating point exception

SIGKILL 9 Term Kill signal

SIGSEGV 11 Core Invalid memory reference

SIGPIPE 13 Term Broken pipe: write to pipe with no

readers

SIGALRM 14 Term Timer signal from alarm(2)

SIGTERM 15 Term Termination signal

SIGUSR1 30,10,16 Term User-defined signal 1

SIGUSR2 31,12,17 Term User-defined signal 2

SIGCHLD 20,17,18 Ign Child stopped or terminated

SIGCONT 19,18,25 Cont Continue if stopped

SIGSTOP 17,19,23 Stop Stop process

SIGTSTP 18,20,24 Stop Stop typed at terminal

SIGTTIN 21,21,26 Stop Terminal input for background process

SIGTTOU 22,22,27 Stop Terminal output for background process



3、trap:捕捉信號,

它不能捕捉9號信號(SIGKILL)和15號信號(SIGTERM),一般常用的捕捉信號是HUP和INT信號。

可以自定義捕捉信號後的處理方法


4、trap命令用法:

格式:trap 'command ' signal //command為捕捉信號後要執行的命令或腳本或函數,多個命令之間用分號(;)隔開;signal為要捕捉的信號


示例1:

[root@localhost sh]# cat tr.sh

#!/bin/bash

trap 'echo "quit";exit' INT //一般把trap寫在最上面,多個command之間用分號(;)隔開,

for i in {1..10};do

ping -W 1 -c 1 192.168.0.$i

done

[root@localhost sh]#


示例二:

























16.2、信號捕捉