help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] wrapper + "exec" + cleanup?


From: Patrick Schleizer
Subject: [Help-bash] wrapper + "exec" + cleanup?
Date: Fri, 29 May 2015 16:50:44 +0000

Hi!

I would like to use "exec" and still be able to cleanup thereafter. I am
aware, this is not how "exec" works, so I am looking for a solution that
is as close as possible.

The use case is roughly:

- A wrapper bash script that,
- 1) does some initial work (ex: temporary config file creation),
- 2) start application,
- 3) cleanup (ex: delete temporary config file).

"exec" works almost perfect for the use case I am interested in.

"exec" gets rid of the pid, signal processing is perfectly left to the
wrapped application, exit codes by wrapped application are preserved.

Also something like the following works...
./wrapper & pid=$! ; sleep 5 ; kill -sigterm $pid
The signal sigterm would be processed by the application.

Only issue: the script does not continue after exec (I know, this is
expected.). There is no way to cleanup after exec, i.e. there is no way
to run an EXIT trap after "exec".

For the cleanup, I would have to avoid using "exec". EXIT traps work
well for those.

But what about signal handling? I tried something like following pseudo
code...

#!/bin/bash
## initial work...
## ... trap exit_handler EXIT ...
./application &
wait $!

But when running...
./wrapper & pid=$! ; sleep 5 ; kill -sigterm $pid
Then $pid isn't the pid of the application, but the pid of the wrapper.
Therefore the signal isn't processed by the application.

I could save the pid of the application inside the wrapper script and
set up signal traps for every signal, then forward those signals to the
application. But perhaps there is a more elegant way to handle this?

Cheers,
Patrick



reply via email to

[Prev in Thread] Current Thread [Next in Thread]