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