Linux命令trap - 在脚本中处理信号 例子

news/2024/5/18 21:51:07 标签: 脚本, linux, command, signal, shell, each

     在Linux中,trap命令主要用于接收信号并采取行动,信号是异步发送到一个程序的事件,在默认情况下,可以终止一个程序,trap命令原型如下:

trap command signal

signal是指接收到的信号,command是接收到该信号采取的行动。如下为两种简单的信号。

信号

说明

INT(2)

Ctrl + C

QUIT(3)

Ctrl + \

trap命令常见的用途在于脚本程序中断时完成清理工作,比如临时文件等。例子程序如下:

#!/bin/sh

trap 'rm -f /tmp/my_tmp_file_$$' INT

echo "creating file /tmp/my_tmp_file_$$"

date > /tmp/my_tmp_file_$$

echo "press interrupt (CTRL-C) to interrupt ......"

while [ -f /tmp/my_tmp_file_$$ ]; do

       echo "File exists"

       sleep 3

done

echo" we never get here"

exit 0

注:$$表示当前的shell进程号。运行该脚本,首先在tmp下创建临时文件my_tmp_file_$$,如果遇到INT中断,即Ctrl+c,即删除该文件。

    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.)




http://www.niftyadmin.cn/n/1736108.html

相关文章

用shc加密shell脚本 - linux进阶屋 - 51CTO技术博客

Shc可以用来对shell脚本进行加密&#xff0c;可以将shell脚本转换为一个可执行的二进制文件。经过shc对shell脚本进行加密后&#xff0c;会同时生成两种个新的文件&#xff0c;一个是加密后的可执行的二进制文件&#xff08;文件名以.x结束&#xff09;&#xff0c;另一个是C语…

利用bootstrap Grid栅格系统的向下缩小特性排版

我们都知道&#xff0c;bootstrap栅格系统为我们提供了.col-xs*,.col-sm-,.col-md-,.col-lg-* 四种布局类来方便我们快速构建响应式布局。 四种类对应的屏幕最小宽度如下&#xff1a; 现在设想一下&#xff0c;我们有一块布局&#xff0c;需要在小屏幕上显示为两栏布局&#x…

python 笔记 数与字符串

《简明 Python 教程》为 "A Byte of Python" 的唯一指定简体中文译本&#xff0c;版权 © 2005 沈洁元 1.数 在Python中有4种类型的数——整数、长整数、浮点数和复数。 2是一个整数的例子。长整数不过是大一些的整数。3.23和52.3E-4是浮点数的例子。E标记表…

javascript中的变量声明原则

1、最小全局变量原则&#xff1a;每一个变量都只应该在其作用域中使用&#xff0c;以避免全局污染和命名冲突&#xff1b; 2、最好的定义变量的方法是显式地使用“var”关键字来定义&#xff1b; 3、通过var关键字声明的变量是无法被“delete”操作符删除的&#xff0c;而没有…

css 权重及!important

在编写css样式的时候&#xff0c;我们会时常碰到自己写的样式没有生效&#xff0c;尤其是引用一些外部框架的时候&#xff0c;这种情况更容易发生。 CSS样式的优先级 按照官方的表述&#xff0c;css优先级如下&#xff1a; 通用选择器&#xff08;*&#xff09; < 元素(类…

python 控制流

简明 Python 教程 Swaroop, C. H. 著 沈洁元 译 运算符通常由左向右结合&#xff0c;即具有相同优先级的运算符按照从左向右的顺序计算。例如&#xff0c;2 3 4被计算成(2 3) 4。一些如赋值运算符那样的运算符是由右向左结合的&#xff0c;即a b c被处理为a (b c)。…

为你的网站开启 gzip 压缩功能(nodejs、nginx)

开启网站的 gzip 压缩功能&#xff0c;通常可以高达70%&#xff0c;也就是说&#xff0c;如果你的网页有30K&#xff0c;压缩之后就变成9K&#xff0c; 对于大部分网站&#xff0c;显然可以明显提高浏览速度&#xff08;注&#xff1a;需要浏览器支持&#xff09;。 nodejs e…

机器学习20:嵌入-Embeddings

嵌入&#xff08;Embeddings&#xff09;是一个相对低维的空间&#xff0c;我们可以将高维向量转换到其中。嵌入使得对大型输入&#xff08;例如表示单词的稀疏向量&#xff09;进行机器学习变得更加容易。理想情况下&#xff0c;嵌入通过将语义相似的输入紧密地放置在嵌入空间…