用途说明

trap是一个shell内建命令,它用来在脚本中指定信号如何处理。比如,按Ctrl+C会使脚本终止执行,实际上系统发送了SIGINT信号给脚本进程,SIGINT信号的默认处理方式就是退出程序。如果要在Ctrl+C不退出程序,那么就得使用trap命令来指定一下SIGINT的处理方式了。trap命令不仅仅处理Linux信号,还能对脚本退出(EXIT)、调试(DEBUG)、错误(ERR)、返回(RETURN)等情况指定处理方式。

常用参数

trap [-lp] [[arg] sigspec ...]

格式:trap "commands" signals

当shell接收到signals指定的信号时,执行commands命令。(The  command arg is to be read and executed when the shell receives signal(s) sigspec. )

格式:trap signals

如果没有指定命令部分,那么就将信号处理复原。比如 trap INT 就表明恢复Ctrl+C退出。(If arg is absent (and there is a single sigspec) or -, each specified signal is reset to its  original  disposition  (the value  it  had  upon  entrance  to  the  shell). )

格式:trap "" signals

忽略信号signals,可以多个,比如 trap "" INT 表明忽略SIGINT信号,按Ctrl+C也不能使脚本退出。又如 trap "" HUP 表明忽略SIGHUP信号,即网络断开时也不能使脚本退出。(If arg is the null string the signal specified by each sigspec is ignored by the shell and by the commands it invokes. )

格式:trap -p

格式:trap -p signal

把当前的trap设置打印出来。(If arg is not present and -p  has  been supplied,  then  the trap commands associated with each sigspec are displayed.  If no arguments are supplied or if only -p is given, trap prints the list of commands associated  with  each  signal.)

格式:trap -l

把所有信号打印出来。(The  -l option  causes  the shell to print a list of signal names and their corresponding numbers.  Each sigspec is either a signal name defined in <signal.h>, or a signal number.  Signal names  are  case  insensitive and  the  SIG prefix is optional.)

格式:trap "commands" EXIT

脚本退出时执行commands指定的命令。(If a sigspec is EXIT (0) the command arg is executed on exit from the shell.)

格式:trap "commands" DEBUG

在脚本执行时打印调试信息,比如打印将要执行的命令及参数列表。(If a sigspec is DEBUG, the command arg is executed before every  simple  command,  for  command, case  command,  select command, every arithmetic for command, and before the first command executes in a shell function (see SHELL GRAMMAR above).  Refer to the description of the extdebug option to the  shopt builtin  for  details of its effect on the DEBUG trap.)

格式:trap "commands" ERR

当命令出错,退出码非0,执行commands指定的命令。(If a sigspec is ERR, the command arg is executed whenever a simple command has a non-zero exit status, subject to the following conditions.  The ERR trap is not executed if the failed command is part of the command list immediately following a while or until keyword, part of the test in an if statement, part of a && or ┅Ι│ list, or if the command’s return  value is  being  inverted via !.  These are the same conditions obeyed by the errexit option.)

格式:trap "commands" RETURN

当从shell函数返回、或者使用source命令执行另一个脚本文件时,执行commands指定的命令。(If a sigspec is RETURN, the command arg is executed each time a shell function or a script executed with the . or source    builtins  finishes  executing.   Signals  ignored  upon  entry  to the shell cannot be trapped or reset. Trapped signals that are not being ignored are reset to their original values in a child process when it is created.  The return status is false if any sigspec is invalid; otherwise trap returns true.)

使用示例

示例一

[root@new55 ~]# trap -p 
[root@new55 ~]# trap "echo hello" INT 
[root@new55 ~]# trap -p 
trap -- 'echo hello' SIGINT
[root@new55 ~]# trap -p INT 
trap -- 'echo hello' SIGINT
[root@new55 ~]# trap -p QUIT

[root@new55 ~]# Ctrl+C

[root@new55 ~]# hello

[root@new55 ~]#

示例二

[root@new55 ~]# trap -l 
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
 9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGSTKFLT
17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU
25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
29) SIGIO       30) SIGPWR      31) SIGSYS      34) SIGRTMIN
35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4
39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX
[root@new55 ~]#

示例三 在脚本中使用

下面的脚本用于检查和启动java程序。

第7行:找出正在运行的符合指定特征的进程;

第11行:如果找到了这样的进程,就杀掉;

第22行:以后台方式启动java程序;

第24行:得到刚启动的程序的pid;

第28行:对SIGTERM信号设置处理方式:结束启动的java程序;

第30行:等待后台进程结束。

Bash代码  
  1. #!/bin/sh
  2. #2007.05.06/07
  3. # 增加了杀掉LAST_PID功能
  4. # 增加了脚本退出时杀掉THIS_PID功能
  5. LAST_PID=$(ps -ef|grep 'java.*Zhenjiang'|grep -v grep|awk '{print $2}')
  6. echo "LAST_PID=$LAST_PID"
  7. if [ -n "$LAST_PID" ] &&  [ "$LAST_PID" -gt 0 ]; then
  8. echo "LAST PROCESS NOT EXIT, NOW KILL IT!"
  9. kill $LAST_PID
  10. sleep 1
  11. fi
  12. if ! cd ../opt/zhenjiang; then
  13. echo "CHANGE DIRECTORY FAILED"
  14. exit 1
  15. fi
  16. java -classpath .:./cpinterfaceapi.jar:./log4j-1.2.14.jar:./hyjc.jar:./smj.client.jar Zhenjiang &
  17. THIS_PID=$!
  18. echo "THIS_PID=$THIS_PID"
  19. trap "kill $THIS_PID" TERM
  20. wait

我使用过的Linux命令之trap - 在脚本中处理信号相关推荐

  1. linux命令 sh -s,shell脚本中 if 判断时候-s是什么意思?

    指的是文件大小非0时为真. shell中怎么判断输入的是否是数字: 第一种:sed格式 首先:我们先(在命令行直接输出模拟一下,如果都正确再在shell脚本中进行书写.)直接echo输出一下 echo ...

  2. 《Linux命令行与shell脚本大全》笔记

    初识Linux Shell 什么是Linux Linux可划分为以下四部分: Linux内核 GNU工具 图形化桌面环境 应用软件 深入探究Linux内核 内核主要负责以下四种功能: 系统内存管理 软 ...

  3. 【Linux】《Linux命令行与shell脚本编程大全 (第4版) 》笔记-Chapter16-脚本控制

    十六.脚本控制 Linux 利用信号与系统中的进程进行通信. 重温 Linux 信号 Linux 系统和应用程序可以产生超过 30 个信号.在 shell 脚本编程时会遇到的最常见的 Linux 系统 ...

  4. 【Linux】《Linux命令行与shell脚本编程大全 (第4版) 》笔记-汇总 ( Chapter17-ChapterB )

    十七.创建函数 bash shell 提供了用户自定义函数功能,可以将 shell 脚本代码放入函数中封装起来. 函数是一个脚本代码块,你可以为其命名并在脚本中的任何位置重用它.每当需要在脚本中使用该 ...

  5. linux 命令行与shell脚本编程大全

    linux 命令行与shell脚本编程大全 第一章 Linux LiveCD就是从cd读取的系统,由于没法将数据写入到cd,所以一旦重启,之前操作过后的一切数据都会丢失. 第二章 第三章 1.man手 ...

  6. linux脚本求命令行上整数和,《Linux命令行与shell脚本编程大全》 第二十二章 学习札记...

    <Linux命令行与shell脚本编程大全> 第二十二章 学习笔记 第二十二章:使用其他shell 什么是dash shell Debian的dash shell是ash shell的直系 ...

  7. Linux命令行与shell脚本编程大全:第2版

    <Linux命令行与shell脚本编程大全:第2版> 基本信息 作者: (美)布卢姆(Blum,R.) 布雷斯纳汉(Bresnahan.C.) [作译者介绍] 译者: 武海峰 丛书名: 图 ...

  8. 《Linux命令行与shell脚本编程大全 第3版》Shell脚本编程基础---34

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: 转载于:https://www.cnbl ...

  9. Linux命令行与shell脚本编程大全(第3版)

    作者:[美] 布鲁姆(Richard Blum),布雷斯纳汉(Christine Bresnahan) 著,门佳,武海峰 译 出版社:人民邮电出版社 品牌:iTuring 出版时间:2016-08-0 ...

最新文章

  1. Java归去来第2集:利用Eclipse创建Maven Web项目
  2. linux rpm找不到命令_linux环境下 python环境import找不到自定义的模块
  3. HTML5 叠加布局
  4. 2017-2018-1 20155327 实验五 通讯协议设计
  5. 送福利 | 送书3本 ASP.NET Core 真机拆解
  6. mysql数据库的目录_了解MySQl数据库目录
  7. lcd1602c语言程序分析,51单片机驱动LCD1602程序设计(C语言).doc
  8. 使用SVN将项目从服务器下载到本地
  9. vmware workstation 9注册码
  10. python计算化学浓度_理论与计算化学 - 计算模拟 - 程序代码 - 小木虫论坛-学术科研互动平台...
  11. pci-e服务器显卡性能,PCI-E插槽速度可影响游戏性能
  12. 卡麦基梅隆大学计算机排名,卡内基梅隆大学,美国国内排名第25位
  13. 个税计算器 / 微信小程序开发
  14. linux 下安装redis并设置开机自启动
  15. git 删除文件后如何恢复此文件
  16. android 4g ram够么,4G还不够,安卓手机内存极限是多少
  17. 【转载】Shell图形化监控网络流量
  18. python做房源饼状图_python使用matplotlib画饼状图
  19. java信息化平台_Java开源企业信息化平台O2OA接入企业钉钉
  20. 1029:三角形的判定

热门文章

  1. 文件上传的单元测试怎么写?
  2. StackOverflow 上面最流行的 7 个 Java 问题!
  3. 化腐朽为神奇:推荐一个让算法动起来更好理解的学习项目!
  4. 如何利用 Arthas 热更新线上代码
  5. 疑案追踪:Spring Boot内存泄露排查记
  6. Spring Boot 2.0 新特性(二):新增事件ApplicationStartedEvent
  7. PS修改过图片上传服务器,富文本编辑器图片上传base64存数据库改为服务器图片路径...
  8. linux触摸屏两指缩放_自定义TextView实现单指拖动,双指缩放旋转
  9. 微信小程序隐藏标题栏navigationBar的方法
  10. 点云分割 PointCloudSegmentation测试笔记