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

问题描述

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

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

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

When my script is executing, if I temporarily suspend from execution, send a SIGSTOP signal+, 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 +. However, when I press + 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标准:

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

09-05 18:29