How to close a Process and having shutdown hooks called ?

Peter Levart peter.levart at gmail.com
Tue Mar 4 14:47:19 UTC 2014


On 03/04/2014 02:09 PM, LE PICARD NICOLAS wrote:
> I didn't know for jdk8 functions destroy and the new one destroyForcibly...
>
> And Jonathan was right in his previous answer, I was looking for a solution in Java, I mean a portable one like  in Process class, like the "destroyGracefully".
> I am looking to signals because it seems to be a simple way to achieve this "portable" solution, maybe I'm wrong.
>
> It would be useful (as you said) to have a function to kill "gracefully" through Process class, no ?
>
> Nicolas Le Picard

Hi Nicolas,

So you're spawning a Java sub-process from a Java master process using 
java.lang.Process, do I understand correctly?

Are you in control of the code that represents the child sub-process? Is 
this code already using System.in (STDIN) stream for anything? If the 
answers are YES and NO respectively, then you may be able to use the 
System.in in the sub-process (spawn a special monitoring thread) and 
Process.getOutputStream() in the master process to establish a one way 
channel for passing commands in direction: master process -> 
sub-process. One of those commands could be "shutdown" for example, that 
would trigger a System.exit() in the sub-process. This way you could 
make the shut-down hooks in sub-process be executed before exiting...

Regards, Peter

>
> -----Message d'origine-----
> De : Alan Bateman [mailto:Alan.Bateman at oracle.com]
> Envoyé : mardi 4 mars 2014 13:12
> À : Krystal Mok
> Cc : LE PICARD NICOLAS; core-libs-dev at openjdk.java.net
> Objet : Re: How to close a Process and having shutdown hooks called ?
>
> On 04/03/2014 11:51, Krystal Mok wrote:
>> Hi Nicolas,
>>
>> Looks like a well discussed question. On Posix systems, SIGTERM should
>> work for you. That's the default signal sent by the 'kill' command on Linux.
>> e.g. please take a look here:
>> http://stackoverflow.com/questions/2541597/how-to-gracefully-handle-th
>> e-sigkill-signal-in-java
>>
>> - Kris
>>
> I think he's on Windows and is looking for destroy to use something other than TerminateProcess.
>
> As background, the destroy method was confusingly specified to kill the sub-process forcibly but it wasn't implemented this way everywhere. On Unix/Linux then the long standing implementation used SIGTERM and so no guarantee that it would cause the sub-process to terminate. This mismatch was examined in JDK 8 (you'll need to go through the archives of this mailing list to see the discussion) and the javadoc updated to make it clear that it is implementation specific as to whether it is done forcibly or not. In addition a new destroyForcibly was added to do the SIGKILL or TerminateProcess for cases where you really want to kill the child. There isn't a corresponding destroyGracefully but clearly this would be useful if were feasible to implement everywhere.
>
> -Alan.




More information about the core-libs-dev mailing list