Gracefully ending bg processes

Eugene Krel

sunmulA
Joined
3/3/08
Messages
399
Points
28
I have gotten used to ending background processes by issuing sigterm, but recently I have been working on a java project that requires a bit of clean up on shutdown. The solution seemed to be to use shutdown hooks, but they appear to have a problem with threaded applications because the actual clean up process seems to vary from time to time in terms of how much of it actually takes place.

If i run the process in the foreground and send input to end the process it executes the clean up correctly and then also throws the hook and runs the clean up correctly.

How do you guys normally end bg processes gracefully?
 
I have gotten used to ending background processes by issuing sigterm, but recently I have been working on a java project that requires a bit of clean up on shutdown. The solution seemed to be to use shutdown hooks, but they appear to have a problem with threaded applications because the actual clean up process seems to vary from time to time in terms of how much of it actually takes place.

If i run the process in the foreground and send input to end the process it executes the clean up correctly and then also throws the hook and runs the clean up correctly.

How do you guys normally end bg processes gracefully?

I don't have a lot of experience with Java, but I can say what I do in C or Perl. In both of them, I setup signal handlers.
In such a function, I consider the thread pool and other downstream dependencies. Basically if your threads ids are collected in a structure, then it's not difficult to wait/terminate them.

The problems arise when your application has multiple processes and multiple threads. In this case, you have to do some IPC. It can be forwarding SIGTERM to all other related processes and they would have signal handling functions.

If you provide more details about the use-case I am sure you will get many replies :)


PS: I assume that you don't use any framework for your application (e.g Spring)
 
Well I am not particularly worried about the child threads as they die when the parent JVM is killed. What is annoying me is the varying behavior of the shutdown hook and it's potential relation to the child threads.
 
Back
Top Bottom