Shell脚本使用陷阱获取CTRL + Z [英] Shell Script get CTRL+Z with Trap

查看:485
本文介绍了Shell脚本使用陷阱获取CTRL + Z的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在脚本的trap中获取SIGSTOP CTRL + Z 信号.

I am trying to get the SIGSTOP CTRL+Z signal in my script's trap.

我的脚本正在执行时,如果我暂时中止,请发送SIGSTOP信号 CTRL + Z ,它需要删除我在其中创建文件并终止执行.

When my script is executing, if I temporarily suspend from execution, send a SIGSTOP signalCTRL+Z, it needs to remove the files I create in it and to kill the execution.

我不明白为什么以下脚本不起作用.但是,更重要的是,正确的方法是什么?

I don't understand why the following script doesn't work. But, more important, what is the correct way to do it?

#!/bin/bash

DIR="temp_folder"
trap "rm -r $DIR; kill -SIGINT $$" SIGSTP

if [ -d $DIR ]
then
    rm -r $DIR
else
    mkdir $DIR
fi
sleep 5

编辑:

SIGSTOP不能被捕获,但是SIGTSTP可以被捕获,根据

SIGSTOP cannot be trapped, however SIGTSTP can be trapped, and from what I understood after searching on the internet and the answer below it's the correct to trap when sending signal with CTRL+Z. However, when I press CTRL+Z while running the script it will get stuck, meaning that the script will be endlessly execute no matter what signals I send afterwards.

推荐答案

有两个信号你不能trap *,SIGKILLSIGSTOP.使用其他信号.

There are two signals you can't trap*, SIGKILL and SIGSTOP. Use another signal.

*:不修改内核

IEEE标准:

设置SIGKILL或SIGSTOP的陷阱会产生不确定的结果.

Setting a trap for SIGKILL or SIGSTOP produces undefined results.

这篇关于Shell脚本使用陷阱获取CTRL + Z的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆