How to close a Process and having shutdown hooks called ?

LE PICARD NICOLAS N.LEPICARD at chu-tours.fr
Tue Mar 4 15:05:43 UTC 2014


Exactly !

In fact, I'm in mastery of ... nothing ! But I tried to workaround this problem and found that Java Process class lacks something ! I was working in Eclipse and you can process while testing (the red button in console view) but shutdown hooks are not called. So I dug a little to understand why Eclipse team said that it is not possible (up to now). Indeed, Eclipse spawns a new Process Object (a new JVM in fact) but can't close it gracefully. I think other application / programs  may need such functionnality so I post on this list to have advices of specialists of openJdk.

You are right again on the second part ! Actually I did exactly that in my applications, only for debug/test, very simple :
		boolean loopz = true;
		InputStreamReader isr = null;
		BufferedReader br = null;
		try {
			isr = new InputStreamReader(System.in);
		 	br = new BufferedReader(isr);
			while (loopz) {
				String userInput = br.readLine();
				System.out.println("input => "+userInput);
				if (userInput.equalsIgnoreCase("quit") || userInput.equalsIgnoreCase("exit")) {
			        		System.exit(0);
				}
			}
		}
		catch (Exception er) {
			er.printStackTrace();
			loopz = false;
		}
		finally {
			try { br.close(); } catch (Exception e) {}
			try { isr.close(); } catch (Exception e) {}
		}


However I think it could be a good add to Java to make a Process having better control on its own child termination.

Thanks !

Nicolas Le Picard


-----Message d'origine-----
De : Peter Levart [mailto:peter.levart at gmail.com] 
Envoyé : mardi 4 mars 2014 15:47
À : LE PICARD NICOLAS; Alan Bateman; Krystal Mok
Cc : core-libs-dev at openjdk.java.net
Objet : Re: How to close a Process and having shutdown hooks called ?

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-t
>> h
>> 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