打开新的Shell会话时,我的Bash脚本不响应信号 [英] My Bash script doesn't respond to SIGNALS when opening new shell session

查看:117
本文介绍了打开新的Shell会话时,我的Bash脚本不响应信号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的bash脚本:

I have this simple bash script:

#!/bin/bash
trap "rm /testfile; service apache2 stop;" SIGTERM
service apache2 start
# Here I want to create new bash session for some reasons.
/bin/bash

现在,某些其他程序可以在发送时使用SIGTERM 到此脚本,但是实际上该脚本没有响应该信号,它一直挂在新的bash会话中,与我使用 sleep infinity 而不是打开新的bash会话(这只是为了测试,因为我需要该bash会话)。

Now some another program can somewhen send SIGTERM to this script, but actually the script does NOT respond to that signal, it keeps hanging in the new bash session, the same case when I use something else like sleep infinity instead of opening new bash session (that was just for testing because I need that bash session).

如何使该脚本响应该SIGTERM?不管新bash会话如何终止,我只想确保在 trap中编写的内容将被执行。

How can I make this script response to that SIGTERM? It doesn't matter how the new bash session will be terminated, I just want to make sure that what I write in trap will be executed.

推荐答案

直到定义了该方法的shell获得控制权后,陷阱才会被调用。由于您启动bash并控制了这个新过程,因此永远不会调用该陷阱。

The trap is not called until the shell in which it is defined gets control back. Since you launch bash and this new process takes control, the trap never gets called.

这篇关于打开新的Shell会话时,我的Bash脚本不响应信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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