trap命令用于指定在接收到信号后将要采取的动作,常见的用途是在脚本程序被中断时完成清理工作。
kill和trap等都可以看到信号编号及其关联的名称。
“信号”是指那些被异步发送到一个程序的事件,默认情况下,它们通常会终止一个程序的运行。
基本格式:
trap command signal
前一部分是接收到指定信号时将要采取的行动,后一部分是要处理的信号名。
1、trap “commands” signal-list
当脚本收到signal-list清单内列出的信号时,trap命令执行双引号中的命令。
2、trap signal-list
trap不指定任何命令,接受信号的默认操作,默认操作是结束进程的运行。
3、trap ” ” signal-list
trap命令指定一个空命令串,允许忽视信号。
注意:
脚本程序通常是以从上到下的顺序解释执行的,所以必须在想保护的那部分代码以前指定trap命令。
信号量详细列表:trap -l 或是 kill -l
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<span style="font-size: 16px;"><span style="color: rgba(0, 128, 128, 1);"> 1</span> <span style="color: rgba(0, 0, 0, 1);">SIGHUP 终止进程 挂起,终端线路挂断 </span><span style="color: rgba(0, 128, 128, 1);"> 2</span> <span style="color: rgba(0, 128, 128, 1);"> 3</span> <span style="color: rgba(0, 0, 0, 1);">SIGINT 终止进程 中断进程 </span><span style="color: rgba(0, 128, 128, 1);"> 4</span> <span style="color: rgba(0, 128, 128, 1);"> 5</span> <span style="color: rgba(0, 0, 0, 1);">SIGQUIT 建立CORE文件 退出,终止进程,并且生成core文件 </span><span style="color: rgba(0, 128, 128, 1);"> 6</span> <span style="color: rgba(0, 128, 128, 1);"> 7</span> <span style="color: rgba(0, 0, 0, 1);">SIGILL 建立CORE文件 非法指令 </span><span style="color: rgba(0, 128, 128, 1);"> 8</span> <span style="color: rgba(0, 128, 128, 1);"> 9</span> <span style="color: rgba(0, 0, 0, 1);">SIGTRAP 建立CORE文件 跟踪自陷 </span><span style="color: rgba(0, 128, 128, 1);">10</span> <span style="color: rgba(0, 128, 128, 1);">11</span> <span style="color: rgba(0, 0, 0, 1);">SIGABRT 程序自己发现错误并调用abort时产生 </span><span style="color: rgba(0, 128, 128, 1);">12</span> <span style="color: rgba(0, 128, 128, 1);">13</span> <span style="color: rgba(0, 0, 0, 1);">SIGBUS 建立CORE文件 总线错误 </span><span style="color: rgba(0, 128, 128, 1);">14</span> <span style="color: rgba(0, 128, 128, 1);">15</span> <span style="color: rgba(0, 0, 0, 1);">SIGSEGV 建立CORE文件 段非法错误 </span><span style="color: rgba(0, 128, 128, 1);">16</span> <span style="color: rgba(0, 128, 128, 1);">17</span> <span style="color: rgba(0, 0, 0, 1);">SIGFPE 建立CORE文件 浮点异常 </span><span style="color: rgba(0, 128, 128, 1);">18</span> <span style="color: rgba(0, 128, 128, 1);">19</span> SIGIOT 建立CORE文件 执行I/<span style="color: rgba(0, 0, 0, 1);">O自陷 </span><span style="color: rgba(0, 128, 128, 1);">20</span> <span style="color: rgba(0, 128, 128, 1);">21</span> <span style="color: rgba(0, 0, 0, 1);">SIGKILL 终止进程 杀死进程 </span><span style="color: rgba(0, 128, 128, 1);">22</span> <span style="color: rgba(0, 128, 128, 1);">23</span> <span style="color: rgba(0, 0, 0, 1);">SIGPIPE 终止进程 向一个没有读进程的管道写数据 </span><span style="color: rgba(0, 128, 128, 1);">24</span> <span style="color: rgba(0, 128, 128, 1);">25</span> <span style="color: rgba(0, 0, 0, 1);">SIGALARM 终止进程 报警,计时器到时 </span><span style="color: rgba(0, 128, 128, 1);">26</span> <span style="color: rgba(0, 128, 128, 1);">27</span> <span style="color: rgba(0, 0, 0, 1);">SIGTERM 终止进程 软件终止信号,通常在系统关机时发送 </span><span style="color: rgba(0, 128, 128, 1);">28</span> <span style="color: rgba(0, 128, 128, 1);">29</span> <span style="color: rgba(0, 0, 0, 1);">SIGSTOP 停止进程 非终端来的停止信号 </span><span style="color: rgba(0, 128, 128, 1);">30</span> <span style="color: rgba(0, 128, 128, 1);">31</span> <span style="color: rgba(0, 0, 0, 1);">SIGTSTP 停止进程 终端来的停止信号 </span><span style="color: rgba(0, 128, 128, 1);">32</span> <span style="color: rgba(0, 128, 128, 1);">33</span> <span style="color: rgba(0, 0, 0, 1);">SIGCONT 忽略信号 继续执行一个停止的进程 </span><span style="color: rgba(0, 128, 128, 1);">34</span> <span style="color: rgba(0, 128, 128, 1);">35</span> SIGURG 忽略信号 I/<span style="color: rgba(0, 0, 0, 1);">O紧急信号 </span><span style="color: rgba(0, 128, 128, 1);">36</span> <span style="color: rgba(0, 128, 128, 1);">37</span> SIGIO 忽略信号 描述符上可以进行I/<span style="color: rgba(0, 0, 0, 1);">O </span><span style="color: rgba(0, 128, 128, 1);">38</span> <span style="color: rgba(0, 128, 128, 1);">39</span> <span style="color: rgba(0, 0, 0, 1);">SIGCHLD 忽略信号 当子进程停止或退出时通知父进程 </span><span style="color: rgba(0, 128, 128, 1);">40</span> <span style="color: rgba(0, 128, 128, 1);">41</span> <span style="color: rgba(0, 0, 0, 1);">SIGTTOU 停止进程 后台进程写终端 </span><span style="color: rgba(0, 128, 128, 1);">42</span> <span style="color: rgba(0, 128, 128, 1);">43</span> <span style="color: rgba(0, 0, 0, 1);">SIGTTIN 停止进程 后台进程读终端 </span><span style="color: rgba(0, 128, 128, 1);">44</span> <span style="color: rgba(0, 128, 128, 1);">45</span> <span style="color: rgba(0, 0, 0, 1);">SIGXGPU 终止进程 CPU时限超时 </span><span style="color: rgba(0, 128, 128, 1);">46</span> <span style="color: rgba(0, 128, 128, 1);">47</span> <span style="color: rgba(0, 0, 0, 1);">SIGXFSZ 终止进程 文件长度过长 </span><span style="color: rgba(0, 128, 128, 1);">48</span> <span style="color: rgba(0, 128, 128, 1);">49</span> <span style="color: rgba(0, 0, 0, 1);">SIGWINCH 忽略信号 窗口大小发生变化 </span><span style="color: rgba(0, 128, 128, 1);">50</span> <span style="color: rgba(0, 128, 128, 1);">51</span> <span style="color: rgba(0, 0, 0, 1);">SIGPROF 终止进程 统计分布图用计时器到时 </span><span style="color: rgba(0, 128, 128, 1);">52</span> <span style="color: rgba(0, 128, 128, 1);">53</span> <span style="color: rgba(0, 0, 0, 1);">SIGUSR1 终止进程 用户定义信号1 </span><span style="color: rgba(0, 128, 128, 1);">54</span> <span style="color: rgba(0, 128, 128, 1);">55</span> <span style="color: rgba(0, 0, 0, 1);">SIGUSR2 终止进程 用户定义信号2 </span><span style="color: rgba(0, 128, 128, 1);">56</span> <span style="color: rgba(0, 128, 128, 1);">57</span> SIGVTALRM 终止进程 虚拟计时器到时</span> |
杀死所有子进程
因为如果父进程执行完毕后,检测core的进程都没有发现core,所有子进程还存在,这就是传说中的僵尸进程。
所以要杀死父进程所有子进程。
shell> kill -9 0 #杀死脚本自己及衍生出来的子进程
最后贴上源码示例一下:
shell> pa.script.sh
1 2 3 4 5 6 7 8 9 10 |
<span style="font-size: 16px;">#! /bin/<span style="color: rgba(0, 0, 0, 1);">bash #调用sub_script.sh 检测core trap </span><span style="color: rgba(128, 0, 0, 1);">'</span><span style="color: rgba(128, 0, 0, 1);">echo "捕获成功";exit</span><span style="color: rgba(128, 0, 0, 1);">'</span> <span style="color: rgba(128, 0, 128, 1);">3</span><span style="color: rgba(0, 0, 0, 1);"> echo </span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">当前位置:父脚本</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);"> echo </span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">PID is $$</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);"> bash sub_script.sh #开启子进程脚本</span></span> |
shell> sub_script.sh
1 2 3 4 5 6 7 8 9 10 11 12 |
<span style="font-size: 16px;">#! /bin/<span style="color: rgba(0, 0, 0, 1);">bash #</span><span style="color: rgba(0, 0, 255, 1);">this</span> shell script <span style="color: rgba(0, 0, 255, 1);">is</span> designed to <span style="color: rgba(0, 0, 255, 1);">catch</span> core,<span style="color: rgba(0, 0, 255, 1);">if</span><span style="color: rgba(0, 0, 0, 1);"> corehappens,will send the father to exit echo </span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">当前脚本:子脚本</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);"> echo </span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">PID is $$</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);"> echo </span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(128, 0, 0, 1);">PPID is $PPID</span><span style="color: rgba(128, 0, 0, 1);">"</span><span style="color: rgba(0, 0, 0, 1);"> sleep </span><span style="color: rgba(128, 0, 128, 1);">2</span><span style="color: rgba(0, 0, 0, 1);"> kill </span>-<span style="color: rgba(128, 0, 128, 1);">3</span> $PPID</span> |