![捕获 ctrl-z 键以执行清理](https://linux22.com/image/1709475/%E6%8D%95%E8%8E%B7%20ctrl-z%20%E9%94%AE%E4%BB%A5%E6%89%A7%E8%A1%8C%E6%B8%85%E7%90%86.png)
大家好,
我有一个 shell 脚本,我想在其中调用一个函数,当用户按下 ctrl-z 键(SIGTSTP 信号)时进行一些清理。我阅读了 trap 命令,并找到了一个可以捕获 ctrl-c 键的示例。有没有办法拦截 SIGTSTP 信号?
答案1
#!/bin/bash
# ctrl + z handler
function suspendHandle() {
echo "$@"
}
# trap the SIGTSTP signal
# suspendHandle is a handler function with the parameters "trapping ctrl + z"
trap "suspendHandle trapping ctrl + z" 20
# send SIGTSTP signal to current shell
kill -s 20 $$