码迷,mamicode.com
首页 > 系统相关 > 详细

shell Trap命令用法

时间:2018-11-17 16:40:57      阅读:647      评论:0      收藏:0      [点我收藏+]

标签:aaa   and   receive   ctrl   shel   fun   for   pre   while   

[root@localhost 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
[root@localhost prog]#

  

[root@localhost 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

  

[root@localhost 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
[root@localhost 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

  

shell Trap命令用法

标签:aaa   and   receive   ctrl   shel   fun   for   pre   while   

原文地址:https://www.cnblogs.com/uxiuxi/p/9974511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!