Linux的trap命令

1. Linux信号

Linux系统利用信号与系统中的进程进行通信。Linux的常见信号有:

信号

描述

1

SIGHP

挂起进程

2

SIGINT

终止进程

3

SIGQUIT

停止进程

9

SIGKILL

无条件终止进程

15

SIGTERM

尽可能终止进程

17

SIGSTOP

无条件停止进程,但不是终止进程

18

SIGTSTP

停止或暂停进程,但不终止进程

19

SIGCONT

继续运行停止的进程

2. 信号组合键

Ctrl+C组合键会产生SIGINT信号,Ctrl+Z会产生SIGTSTP信号。

3. trap命令

trap命令允许你来指定shell脚本要监视并拦截的Linux信号。trap命令的格式为:trap commands signals

demo1

shell脚本:

#!/bin/bash
# test trap command
trap "echo 'Sorry! I have trapped Ctrl-C'" SIGINT

echo This is a test script

count=1
while [ $count -le 10 ]
do
  echo "Loop $count"
  sleep 1
  count=$[ $count + 1 ]
done

echo The end.

运行结果:

This is a test script
Loop 1
Loop 2
^CSorry! I have trapped Ctrl-C
Loop 3
Loop 4
^CSorry! I have trapped Ctrl-C
Loop 5
Loop 6
Loop 7
Loop 8
^CSorry! I have trapped Ctrl-C
Loop 9
Loop 10
The end.

demo2

除了在shell脚本中捕获信号外,也可以在shell退出时捕获,在trap命令后加上EXIT信号就行。

shell脚本:

#!/bin/bash
# test trap command
trap "echo Goodbye." EXIT

echo This is a test script

count=1
while [ $count -le 10 ]
do
  echo "Loop $count"
  sleep 1
  count=$[ $count + 1 ]
done

echo The end.

运行结果:

This is a test script
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Loop 6
Loop 7
Loop 8
Loop 9
Loop 10
The end.
Goodbye.

demo3——修改trap操作

#!/bin/bash
# test trap command

trap "echo 'Sorry! I have trapped Ctrl-C'" SIGINT

count=1
while [ $count -le 5 ]
do
  echo "Loop $count"
  sleep 1
  count=$[ $count + 1 ]
done


trap "echo 'Sorry! The trap has been modified.'" SIGINT

count=1
while [ $count -le 5 ]
do
  echo "Loop $count"
  sleep 1
  count=$[ $count + 1 ]
done

echo The end.

运行结果:

Loop 1
Loop 2
Loop 3
^CSorry! I have trapped Ctrl-C
Loop 4
Loop 5
Loop 1
Loop 2
Loop 3
^CSorry! The trap has been modified.
Loop 4
Loop 5
The end.

demo4

删除捕获,命令形式为:trap -- ***,例如trap -- SIGINT

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券