Loading

LINUX trap命令

trap命令

  作用:指定在接收到信号后将要采取的行动。常见用途就是在脚本程序被中断时完成清理工作。
  trap有两个参数,第一个是接收到指定信号时将要采取的行动,第二个是要处理的信号名。
		trap command signal
  如果要重置某个信号的处理方式到默认值,只需将command设置为-。
  如果要忽略某个信号,就把 command设置为空字符串’’。

例子:

#!/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 1
done
echo The file no longer exists

trap INT
echo creating file /tmp/my_tmp_file_$$
date > /tmp/my_tmp_file_$$
echo “press interrupt (control-C) to interrupt ....”
while [ -f /tmp/my_tmp_file_$$ ]; do
	echo File exists
	sleep 1
done
echo we never get here
exit 0

  注意:   INF:中断信号 (CTRL+C)
posted @ 2020-12-19 19:33  IamHzc  阅读(388)  评论(0编辑  收藏  举报