trap

shell收到信号时执行命令。

语法

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

选项

-p   显示信号命令

-l   打印信号名称列表及其对应的数字

arg  shell接收sigspec信号时要执行的命令

每个sigspec都是信号名称或信号编号。信号名称不区分大小写,SIG前缀是可选的


Number	SIG	         Meaning
0	    0	          从shell退出时
1	    SIGHUP	  清理整理
2    	SIGINt	          中断(CTRL-C)
3	    SIGQUIT	  退出
6	    SIGABRT	  中止
9	    SIGKILL	  死亡
14	    SIGALRM	  闹钟
15	    SIGTERM	  终止

如果任何sigspec无效,则返回状态为False ; 否则返回True。

trap是一个内置命令的bash。

例子>

#!/bin/bash

$ SCRATCH=$(mktemp -t tmp.XXXXXXXXXX)

trap cleanup 1 2 3 6

cleanup()
{
  echo "Cleaning up temp files:"
  rm -rf "$SCRATCH"
  exit 1
}

trap 命令实例:

linux命令:trap '' 1 2 20 24(signal number) number) 24(signal 20 2 1 '' trap trap '' 1 2 20 24(signal number)rootopen.com
linux命令:trap '' 1 2 20 24(signal number) number) 24(signal 20 2 1 '' trap trap '' 1 2 20 24(signal number)rootopen.com
2018-03-29 17:49:48

linux命令:trap "echo \"$0 process $$ killed on $(date).\" | tee ${0##*/}_$$_termination.log; echo 'Active processes at the time were logged to ${0##*/}_$$_termination.log'; ps u >> ${0##*/}_$$_termination.log; exit " HUP INT QUIT ABRT TERM STOP STOP TERM ABRT QUIT INT HUP " exit ${0##*/}_$$_termination.log; >> u ps ${0##*/}_$$_termination.log'; to logged were time the at processes 'Active echo ${0##*/}_$$_termination.log; tee | $(date).\" on killed $$ process \"$0 "echo trap trap "echo \"$0 process $$ killed on $(date).\" | tee ${0##*/}_$$_termination.log; echo 'Active processes at the time were logged to ${0##*/}_$$_termination.log'; ps u >> ${0##*/}_$$_termination.log; exit " HUP INT QUIT ABRT TERM STOProotopen.com
linux命令:trap "echo \"$0 process $$ killed on $(date).\" | tee ${0##*/}_$$_termination.log; echo 'Active processes at the time were logged to ${0##*/}_$$_termination.log'; ps u >> ${0##*/}_$$_termination.log; exit " HUP INT QUIT ABRT TERM STOP STOP TERM ABRT QUIT INT HUP " exit ${0##*/}_$$_termination.log; >> u ps ${0##*/}_$$_termination.log'; to logged were time the at processes 'Active echo ${0##*/}_$$_termination.log; tee | $(date).\" on killed $$ process \"$0 "echo trap trap "echo \"$0 process $$ killed on $(date).\" | tee ${0##*/}_$$_termination.log; echo 'Active processes at the time were logged to ${0##*/}_$$_termination.log'; ps u >> ${0##*/}_$$_termination.log; exit " HUP INT QUIT ABRT TERM STOProotopen.com
2018-04-11 14:31:48

linux命令:trap "echo \"$0 process $$ killed on $(date).\"; exit " HUP INT QUIT ABRT TERM STOP STOP TERM ABRT QUIT INT HUP " exit $(date).\"; on killed $$ process \"$0 "echo trap trap "echo \"$0 process $$ killed on $(date).\"; exit " HUP INT QUIT ABRT TERM STOProotopen.com
linux命令:trap "echo \"$0 process $$ killed on $(date).\"; exit " HUP INT QUIT ABRT TERM STOP STOP TERM ABRT QUIT INT HUP " exit $(date).\"; on killed $$ process \"$0 "echo trap trap "echo \"$0 process $$ killed on $(date).\"; exit " HUP INT QUIT ABRT TERM STOProotopen.com
2018-05-01 02:04:48

linux命令:trap cmd 0 0 cmd trap trap cmd 0rootopen.com
linux命令:trap cmd 0 0 cmd trap trap cmd 0rootopen.com
2018-03-20 11:06:48

linux命令:trap 'echo -e "\e]0;$BASH_COMMAND\007"' DEBUG DEBUG "\e]0;$BASH_COMMAND\007"' -e 'echo trap trap 'echo -e "\e]0;$BASH_COMMAND\007"' DEBUGrootopen.com
linux命令:trap 'echo -e "\e]0;$BASH_COMMAND\007"' DEBUG DEBUG "\e]0;$BASH_COMMAND\007"' -e 'echo trap trap 'echo -e "\e]0;$BASH_COMMAND\007"' DEBUGrootopen.com
2018-02-04 02:14:48

linux命令:trap "source ~/.zshrc" USR1 USR1 ~/.zshrc" "source trap trap "source ~/.zshrc" USR1rootopen.com
linux命令:trap "source ~/.zshrc" USR1 USR1 ~/.zshrc" "source trap trap "source ~/.zshrc" USR1rootopen.com
2018-02-15 09:46:48

Linux的Bash命令行(A-Z排序)