Linux -trap

来源:互联网 发布:ubuntu 更新内核 编辑:程序博客网 时间:2024/06/01 07:31

Trap 

The trap command allows you to execute a command when a signal is received by your script. It works like this:

trap arg signals

note: signals is a list of signals to intercept and "arg" is a command to execute when one of the signals is received. 

trap 命令,用来相应信号。其命令格式如下:

trap "command or commands" signal-numbers(or names)

note:命令或者命令组应该放在双引号内,多个命令用分号隔开,多个信号之间用空格分开


trap的三种形式

(1) 如果trap中命令丢失,表示脚本接受信号的默认动作

(2)如果捕获信号的命令参数是空字符串,表示脚本需要忽略这些信号。例如 trap "" 2 3 表示信号2,3将被忽略

(3)如果捕获信号的命令参数包含一个或者一组命令,表示脚本需要捕获信号,并在信号采取自己的动作之前保存一段时间一遍完成某些动作。

如:

trap "cp ~/file1 /root/" 2 3 15

待续。。。

0 0