My Bash script doesn't respond to SIGNALS when

2019-09-14 16:29发布

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条回答
看我几分像从前
2楼-- · 2019-09-14 16:48

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.

查看更多
登录 后发表回答