ksh trap signal
来源:互联网 发布:Excel数据有效性底纹 编辑:程序博客网 时间:2024/05/10 22:18
What's Signal
When a signal is received, a script can do one of three actions:
- Ignore it and do nothing.
- Catch the signal using trap and take appropriate action.
- Take the default action.
All the above is true except for the following signals:
SIGKILL (signal 9)
SIGSTOP (signal 17)
SIGCONT (signal 19)
These cannot be caught and always uses the default action.
Signal Syntax
trap [ command ] signal [ signal ... ]
- Command can be explicit commands or a function
- Command
trap 'echo signal received, now exiting..; exit' 2 6
- Command
- function
function mysignal { echo "in mysignal function" exit}trap 'mysignal' 2 6
- Signal can be a signal number or signal name
Signals numbers from 0 to 31, "0" being a pseudo-signal meaning "program termination", or using name, HUP for HANGUP signal, TERM for the SIGTERM signal etc.
trap 'echo signal received, now exiting..; exit' 2 6trap 'echo signal received, now exiting..; exit' SIGINT SIGQUIT
What will script do when a signal(for example 15) is received:
- The script would trap the signal 15, and execute the command "rm -f $Tmp", thus removing the temporary file.
- it would continue with the next script command. This could cause strange results, because the (probably needed) temporary file $Tmp is gone. Another point is that somebody explicitly tried to terminate the script, a fact it deliberately ignores.
How to ignore and reset default signal handler
To ignore a signal, use two single quotes in place of the command:
trap '' signals
To reset a trap use:
trap - signals
KSH Signal Code Samples
1 #!/bin/ksh 2 3 function mysignal { 4 echo "in mysignal" 5 #exit 6 } 7 8 trap "mysignal" SIGINT SIGQUIT 910 function mainfun {11 sleep 1012 }1314 mainfun1516 echo "End of script"
When script is running, and CTRL-2 is pressed, function mysignal will be called, and after function is finished, control-flow come to the statement immediately after 'mainfun', so the final "End of script" is executed.
So if line 5 (#exit) is not commented out, line 16 will not be executed, because script will exit at line 5.
- ksh trap signal
- linux trap command signal
- trap catch signal
- ksh
- ksh
- 启动java程序报Pid 9899 in trap loop, signal 11,9899 Memory fault(coredump)错误
- (win 7)使用puma以后,重启rails server报错: in `trap': unsupported signal SIGCHLD (ArgumentError)
- ksh学习
- ksh basic
- Ksh版本
- ksh(一)
- ksh 学习
- 回归ksh
- ksh 案例
- KSH 语法
- mp1_update_oracle.ksh
- KSH入门
- 继续ksh
- 欧拉工程第11题 在20×20的网格中同一直线上四个数的最大乘积是多少
- S8 U8 S16_LE S16_BE U16_LE U16_BE格式
- C Primer Plus学习 敲代码的错误1
- [LeetCode]406.Queue Reconstruction by Height
- 自学编程之道
- ksh trap signal
- LeetCode 287. Find the Duplicate Number 解题报告
- linux 下ftp服务器搭建
- 使用sklearn优雅地进行数据挖掘
- 10.11
- csdn如何转载别人的文章
- Java传递参数的问题
- View视图类与Dialog对话框通信实例
- eclipse中自定义异常块