My Bash script doesn't respond to SIGNALS when opening new shell session

℡╲_俬逩灬. 提交于 2019-12-12 02:08:52

问题


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

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).

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.


回答1:


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.



来源:https://stackoverflow.com/questions/41709975/my-bash-script-doesnt-respond-to-signals-when-opening-new-shell-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!