- 浏览: 4732770 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
bzhao:
你也应该可以这样:(not tested)./rbtunnel ...
在Bash脚本中怎么关闭文件描述符? -
bzhao:
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
在Bash脚本中怎么关闭文件描述符? -
Master-Gao:
楼主咋没分析下源码呢?
我使用过的Linux命令之dirname - 截取给定路径的目录部分 -
jiedushi:
tail -F 就可以吧
Linux下实时跟踪log4j日志文件的bash脚本 - 增强了tail -f的功能 -
java_is_new:
新手学习了,就是不明白为一个网卡配多个ip有什么用
我使用过的Linux命令之ifconfig - 网络配置命令
我使用过的Linux命令之trap - 在脚本中处理信号
本文链接:http://codingstandards.iteye.com/blog/836588 (转载请注明出处)
用途说明
rap是一个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行:等待后台进程结束。
#!/bin/sh #2007.05.06/07 # 增加了杀掉LAST_PID功能 # 增加了脚本退出时杀掉THIS_PID功能 LAST_PID=$(ps -ef|grep 'java.*Zhenjiang'|grep -v grep|awk '{print $2}') echo "LAST_PID=$LAST_PID" if [ -n "$LAST_PID" ] && [ "$LAST_PID" -gt 0 ]; then echo "LAST PROCESS NOT EXIT, NOW KILL IT!" kill $LAST_PID sleep 1 fi if ! cd ../opt/zhenjiang; then echo "CHANGE DIRECTORY FAILED" exit 1 fi java -classpath .:./cpinterfaceapi.jar:./log4j-1.2.14.jar:./hyjc.jar:./smj.client.jar Zhenjiang & THIS_PID=$! echo "THIS_PID=$THIS_PID" trap "kill $THIS_PID" TERM wait
问题思考
相关资料
【1】Leo linux trap命令
【2】ME [原]linux下的trap命令
【3】Linux宝库 在shell中捕捉信号的trap命令
【4】ubuntuer zone linux trap详解
发表评论
-
在Linux用tar归档压缩文件时忽略某些文件和目录
2013-02-01 10:19 16927在Linux下,常用tar对文 ... -
使用nmap扫描服务器端口的一次操作
2012-11-01 17:00 15075使用nmap扫描服务器端口的一次操作 本文来自:http ... -
我使用过的Linux命令之wget - ooo
2011-09-14 13:10 0我使用过的Linux命令之wg ... -
推荐一篇学习Vim使用的好文:酷壳 - 简明 Vim 练级攻略
2011-09-09 12:53 9085简明 Vim 练级攻略 http://coolshell.c ... -
推荐一篇学习Vim使用的好文:酷壳 - 简明 Vim 练级攻略
2011-09-09 12:49 1简明 Vim 练级攻略 http://coolshell.c ... -
我使用过的Linux命令之:(冒号) - 啥也不做(除了……)
2011-08-29 13:18 11984我使用过的Linux命令之: ... -
我使用过的Linux命令之date - 显示、修改系统日期时间
2011-08-25 09:21 41905我使用过的Linux命令之da ... -
我使用过的Linux命令之declare - 声明shell变量(不知道没关系、知道了就更好的内建命令)
2011-08-16 09:22 21741我使用过的Linux命令之declare - 声明shell变 ... -
我使用过的Linux命令之alias - 设置命令的别名,让 Linux 命令更简练
2011-08-11 09:31 28717我使用过的Linux命令之alias - 设置命令的别名,让 ... -
我使用过的Linux命令之ar - 创建静态库.a文件
2011-08-08 10:40 51816我使用过的Linux命令之ar - 创建静态库.a文件 本 ... -
我使用过的Linux命令之crontab - 设置例行任务(类似于Windows中的任务计划)
2011-08-04 22:26 9691我使用过的Linux命令之crontab - 设置例行任务(类 ... -
我使用过的Linux命令之chmod - 改变文件/目录的访问权限
2011-08-03 21:33 10641我使用过的Linux命令之chmod - 改变文件/目录的访问 ... -
我使用过的Linux命令之export - 设置或显示环境变量
2011-08-02 19:55 25371我使用过的Linux命令之export - 设置或显示环境变量 ... -
我使用过的Linux命令之wc - 统计文件行数、单词数或字节数
2011-07-26 10:50 28933我使用过的Linux命令之wc - 统计文件行数、单词数或字节 ... -
我使用过的Linux命令之groupdel - 删除群组
2011-07-22 22:13 9620我使用过的Linux命令之gr ... -
我使用过的Linux命令之ifconfig - 网络配置命令
2011-07-21 20:43 60376我使用过的Linux命令之ifconfig - 网络配置命令 ... -
我使用过的Linux命令之ll - 列出文件/目录的详细信息
2011-07-20 21:22 7104我使用过的Linux命令之ll ... -
我使用过的Linux命令之mkdir - 创建目录
2011-07-20 20:58 13605我使用过的Linux命令之mkdir - 创建目录 本文链 ... -
我使用过的Linux命令之perror - 解释错误代码
2011-07-18 20:29 24969我使用过的Linux命令之perror - 解释错误代码 ... -
我使用过的Linux命令之ping - 测试与目标主机的连通性
2011-07-16 10:46 26256我使用过的Linux命令之ping - 测试与目标主机的连通性 ...
相关推荐
rap是一个shell内建命令,它用来在脚本中指定信号如何处理。比如,按Ctrl+C会使脚本终止执行,实际上系统发送了SIGINT信号给脚本进程,SIGINT信号的默认处理方式就是退出程序。如果要在Ctrl+C不退出程序,那么就得...
026_创建以日期命名的文件和临时文件_信号_trap命令以及如何捕获信号_eval命令_logger命令.pdf 027_脚本例子_pingall_backup_gen_del.lines_acces_deny_logroll_nfsdown.pdf 028_rcN.d_运行级别脚本编辑.pdf 029...
14.4.1 在脚本中使用位置参数 142 14.4.2 向系统命令传递参数 142 14.4.3 特定变量参数 143 14.4.4 最后的退出状态 144 14.5 小结 145 第15章 引号 146 15.1 引用必要性 146 15.2 双引号 146 15.3 单引号 147 15.4 ...
14.4.1 在脚本中使用位置参数 142 14.4.2 向系统命令传递参数 142 14.4.3 特定变量参数 143 14.4.4 最后的退出状态 144 14.5 小结 145 第15章 引号 146 15.1 引用必要性 146 15.2 双引号 146 15.3 单引号 147 15.4 ...
14.4.1 在脚本中使用位置参数 142 14.4.2 向系统命令传递参数 142 14.4.3 特定变量参数 143 14.4.4 最后的退出状态 144 14.5 小结 145 第15章 引号 146 15.1 引用必要性 146 15.2 双引号 146 15.3 单引号 147 15.4 ...
4.5.2 在底线模式中的替换命令 4.5.3 底线模式中的删除方式 4.6 撤销和重复编辑命令 4.7 移动和复制文本 4.7.1 文本行的移动 4.7.2 在底线模式中的整行移动 4.7.3 粘贴和复制 4.8 在 vi 编辑器中与 shell交互 4.9 ...
14.4.1 在脚本中使用位置参数 142 14.4.2 向系统命令传递参数 142 14.4.3 特定变量参数 143 14.4.4 最后的退出状态 144 14.5 小结 145 第15章 引号 146 15.1 引用必要性 146 15.2 双引号 146 15.3 单引号 147 15.4 ...
14.4.1 在脚本中使用位置参数 142 14.4.2 向系统命令传递参数 142 14.4.3 特定变量参数 143 14.4.4 最后的退出状态 144 14.5 小结 145 第15章 引号 146 15.1 引用必要性 146 15.2 双引号 146 15.3 单引号 147 15.4 ...
14.4.1 在脚本中使用位置参数 142 14.4.2 向系统命令传递参数 142 14.4.3 特定变量参数 143 14.4.4 最后的退出状态 144 14.5 小结 145 第15章 引号 146 15.1 引用必要性 146 15.2 双引号 146 15.3 单引号 147 15.4 ...
14.4.1 在脚本中使用位置参数 142 14.4.2 向系统命令传递参数 142 14.4.3 特定变量参数 143 14.4.4 最后的退出状态 144 14.5 小结 145 第15章 引号 146 15.1 引用必要性 146 15.2 双引号 146 15.3 单引号 ...
26.5.2 在脚本中使用logger命令 303 26.6 小结 305 第27章 几个脚本例子 306 27.1 pingall 306 27.2 backup_gen 306 27.3 del.lines 312 27.4 access.deny 313 27.5 logroll 316 27.6 nfsdown 317 27.7 小结 317 第...
14.4.1 在脚本中使用位置参数 142 14.4.2 向系统命令传递参数 142 14.4.3 特定变量参数 143 14.4.4 最后的退出状态 144 14.5 小结 145 第15章 引号 146 15.1 引用必要性 146 15.2 双引号 146 15.3 单引号 147 15.4 ...
14.4.1 在脚本中使用位置参数 142 14.4.2 向系统命令传递参数 142 14.4.3 特定变量参数 143 14.4.4 最后的退出状态 144 14.5 小结 145 第15章 引号 146 15.1 引用必要性 146 15.2 双引号 146 15.3 单引号 147 15.4 ...