linux下tarp命令的简单用法

#################信号##################
linux通过信号来在运行在系统上的进程之间通信
也可以通过信号来控制shell的运行

1.捕捉信号:trap命令
常用信号
ctrl+c(终止进程)
ctrl+z(打入后台暂停进程)
trap "echo westos"2 #修改ctrl+c的结果
在这里插入图片描述
trap : 2 #复原
在这里插入图片描述
列出中断信号与键盘的关系
stty -a
在这里插入图片描述
信号屏蔽
[root@server ~]#trap “” 2 #屏蔽
在这里插入图片描述
[root@server ~]# trap : 2 #复原
[root@server ~]# trap - 2 #复原
在这里插入图片描述
2.捕捉脚本的退出

以脚本为例,将ctrl+c结果替换为一段话等待5秒后恢复

#!/bin/bash

trap "echo 'Sorry!I have trapped Ctrl+C'" 2
echo "This is a test script~"

count=1

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

echo "This is the end of the script~"
trap - 2
echo "I just removed the trap"

在这里插入图片描述

3.修改ctrl+c的结果为一个可执行的命令

写脚本自动创建文件并设置ctrl+c为删除全部创建的文件并退出

#!/bin/bash

trap "find /tmp -type f -name "westos_*" | xargs rm -f && exit " 2

while true
do
    touch /tmp/westos_$(date +%F-%N-%M-%S)
    sleep 2
    ls -l /tmp/westos*
done

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值