How to Catch Ctrl-C in Shell Script

Posted 嗡嘛呢巴美吽舍

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了How to Catch Ctrl-C in Shell Script相关的知识,希望对你有一定的参考价值。

 
#!/bin/sh
 
# this function is called when Ctrl-C is sent
function trap_ctrlc ()
{
    # perform cleanup here
    echo "Ctrl-C caught...performing clean up"
 
    echo "Doing cleanup"
 
    # exit shell script with error code 2
    # if omitted, shell script will continue execution
    exit 2
}
 
# initialise trap to call trap_ctrlc function
# when signal 2 (SIGINT) is received
trap "trap_ctrlc" 2
 
# your script goes here
echo "going to sleep"
sleep 1000
echo "end of sleep"

 

以上是关于How to Catch Ctrl-C in Shell Script的主要内容,如果未能解决你的问题,请参考以下文章

sh 来自:http://askubuntu.com/questions/217893/how-to-delete-a-non-empty-directory-in-terminal

在pycharm中以管理员身份运行/调试脚本(How to run / debug programs as root in Pycharm)

sh how-to-deal-command-line-parameters.sh

How can you catch a process that is about to be launched, if you don’t know the PID yet?

How To Use the AWK language to Manipulate Text in Linux

How to Create Triggers in MySQL