加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

shell Trap命令用法

发布时间:2020-12-15 23:13:22 所属栏目:安全 来源:网络整理
导读:[[email?protected] prog]# cat trap2.sh#!/bin/bashfunction cleanup(){ echo "Received signals and cleanup files"}#trap 到SIG信号,自动停止trap ‘cleanup;exit 1‘ SIGHUPtrap ‘cleanup;exit 1‘ SIGINTtrap ‘cleanup;exit 1‘ SIGTRAP#SIGKILL can
[[email?protected] prog]# cat trap2.sh
#!/bin/bash

function cleanup()
{
        echo "Received signals and cleanup files"
}

#trap 到SIG信号,自动停止
trap  ‘cleanup;exit 1‘ SIGHUP
trap  ‘cleanup;exit 1‘ SIGINT

trap  ‘cleanup;exit 1‘ SIGTRAP
#SIGKILL can not be traped
while :
do
        echo "Hi there"
        sleep 1
done
[[email?protected] prog]#

  

[[email?protected] prog]# cat trap4.sh
#!/bin/bash

trap ‘cleanup‘ SIGINT
function cleanup()
{
        echo "call function"
        trap - SIGINT    #遇到一次SIGINT, 就会清除掉trap捕捉SIGINT
}
while :
do
        echo "hello aaa"
        sleep 2
done

  

[[email?protected] prog]# cat trap5.sh
#!/bin/bash

cleanup_new()
{
        echo "Some new and extra cleanup performed"
        trap - SIGINT   #第三次TRAP 加exit。消除SIGINT
}

cleanup_old()
{
        echo "Some cleanup performed"
        trap cleanup_new SIGINT    #第二次继续trap INT
}

trap ‘cleanup_old SIGINT‘ SIGINT  #第一次trap SIGINT

while true
do
        echo ‘hello‘
        sleep 1
done
[[email?protected] prog]#

  

?

?

#!/bin/bash trap "echo trap ctrl+c" SIGINT while : do echo "Hi there" sleep 1 done #通过kill -l 命令可以看到,ctrl+C等同SIGINT,不会打断,但是会trap到所以显示trap ctrl+c

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读