process.destroy() does not kill the subprocess -- any help?

2,665 views
Skip to first unread message

Raghavan.30May1981_GMail

unread,
Jun 9, 2009, 6:01:07 AM6/9/09
to bo...@googlegroups.com
Dear Friends,

I have an issue with destroy() method of java.lang.Process class. All what I am trying to do is, controlling the execution of one program through another. Let's say, Program B has to be executed conditionally through Program A based on the commands it gets from the user. Let's say, we have two inputs, "start" and "stop" to drive the Program B.

I have thought of various alternatives.

(1) Using Thread to control the execution of Program B. But then stopping a process means via a thread is not allowed as the related methods are deprecated (stop/suspend etc.,)
(2) Using ThreadGroup ( to have only one thread as its member) and calling 'destroy' on the group. But it again falls on the same track as every thread should be stopped before the destroy operation is attempeted.
(3) Process/ProcessBuilder via Runtime -- seems to be the better way to obtain a process reference and call destroy(), waitFor() etc., to control the spawned child process.


I went ahead with 3rd approach and all went fine till the spawning of the new child process. When the time came to stop the execution (ideally speaking, to stop! but practically destroying/killing the child process), it breaks!

My main program (Program A) gives an information that the Program B stopped successfully (as I do store the process references in a Map and retrieve the same before destroying the process) and this operation succeeds. But it does NOT really reflect in reality and my child program still runs as if it was never interrupted. Looks like the child process gets disjoint with the parent process and hence the link gets cut between the two. I have to invoke my Program B via "cmd.exe /C java.exe ProgramB". I do use Windows XP and JRE 1.6.0_10 beta.

Program B is a java program which just keeps writing the date and time into a log file on an interval of 1 second. Program A has the main() method and gets the "threadName && start/stop" inputs from the user and retains the information into the Map. I am not dependent on my Program B's output and hence I don't invoke waitFor() and exitValue() methods.

If required I can post the program here.

Having been searching in the internet for a long time but nothing seems to be fruitful. Some says that it may be a bug with JVM. But I don't think so. Few typical examples of this kind is, Tomcat catalina (startup.bat/shutdown.bat) and Quartz Job Scheduler -- they do get a command from the user 'start/stop' to fire an action!

Looks strange as the API does NOT guarantee the behavior as per the Specification. It says,

    public abstract void destroy()  
         Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.   

Do I miss something here? Any suggestions would be of a great help! 

Cheers,
Raghavan alias Saravanan M.
http://groups.google.com/group/S_H_a_D_E

"If at all a single word could efface all the atrocities, I would dare to let my egotism grow and mutilate the people by cozening the belief  first and ask a concomitant SORRY later!" - Raghavan alias Saravanan M.

Vikram A

unread,
Jun 9, 2009, 6:19:22 AM6/9/09
to bo...@googlegroups.com
I had to look up what /C does with a cmd.

/C Carries out the command specified by string and then terminates

Obviously, the process you are trying to run is a cmd (NOT java)
"cmd.exe /C java.exe ProgramB".
now when it starts it just starts off a new child 'java' and ends.
So by the time you call destroy() the child process has already
ended(most likely) and you are trying to kill a non existent or may be
a zombie process.

interestingly though, why do you need to go through a rerouting via cmd,
isn't starting the java process via this work ?
Process p =Runtime.getRuntime().exec("java ......");'
then
p.destroy();
or there might be something else that you need and not apparent to me.

rgds,
-Vikram.

Ranganath s

unread,
Jun 9, 2009, 6:26:42 AM6/9/09
to bo...@googlegroups.com
can you post the code may be that would be more clear...

-R
--
I blog at http://ranganaths.wordpress.com

Raghavan.30May1981_GMail

unread,
Jun 9, 2009, 6:29:07 AM6/9/09
to bo...@googlegroups.com
Hi Vikram,
 
 That was a good insight. Thanks. After looking at this, I had just taken off "cmd.exe /C' switch completely and now my program works so perfectly :). But somehow had been going through this way to start off with "cmd.exe /C" and I believe it may be for dealing with native Win32 OS commands. Bad of me! ;(

  Moreover, there is another related switch called "/K" for cmd.exe which does NOT terminate but remains. Still, the same effect. They both behave the same way! Unable to get them differentiated in this scenario. Any hints here?


Cheers,
Raghavan alias Saravanan M.
http://groups.google.com/group/S_H_a_D_E

"If at all a single word could efface all the atrocities, I would dare to let my egotism grow and mutilate the people by cozening the belief  first and ask a concomitant SORRY later!" - Raghavan alias Saravanan M.


On Tue, Jun 9, 2009 at 3:49 PM, Vikram A <vikram....@gmail.com> wrote:

Sriram Narayanan

unread,
Jun 9, 2009, 6:38:38 AM6/9/09
to bo...@googlegroups.com
I've felt this to be the best :
http://wrapper.tanukisoftware.org/doc/english/download.jsp

You may also want to investigate these two:
http://commons.apache.org/exec/
http://commons.apache.org/daemon/procrun.html -> Tomcat uses this

-- Sriram

Raghavan.30May1981_GMail

unread,
Jun 9, 2009, 6:56:16 AM6/9/09
to bo...@googlegroups.com
Thanks Sriram. Shall look at them :)


Cheers,
Raghavan alias Saravanan M.
http://groups.google.com/group/S_H_a_D_E

"If at all a single word could efface all the atrocities, I would dare to let my egotism grow and mutilate the people by cozening the belief  first and ask a concomitant SORRY later!" - Raghavan alias Saravanan M.


Vikram A

unread,
Jun 9, 2009, 6:59:29 AM6/9/09
to bo...@googlegroups.com
Ah that's interesting.
We always need a exit to close the waiting terminal window, even just
to exit a terminal with /K running a simple batch file.
Let me make a guess here.
Perhaps, what we need to get a handle to the streams of the new
terminal and close them explicitly before calling a destroy.
need to check otherwise.

rgds,
-Vikram.

Sriram Narayanan

unread,
Jun 9, 2009, 7:22:36 AM6/9/09
to bo...@googlegroups.com
On Tue, Jun 9, 2009 at 4:29 PM, Vikram A<vikram....@gmail.com> wrote:
>
> Ah that's interesting.
> We always need a exit to close the waiting terminal window, even just
> to exit a terminal with /K running a simple batch file.
> Let me make a guess here.
> Perhaps, what we need to get a handle to the streams of the new
> terminal and close them explicitly before calling a destroy.
> need to check otherwise.
>

The process launcher too needs to be terminated - In this case, cmd.exe.
I faced these problems, and therefore stopped running Tomcat via a
batch file and went on to write a Swing based GUI app which embedded
Tomcat.

Raghavan.30May1981_GMail

unread,
Jun 9, 2009, 7:39:25 AM6/9/09
to bo...@googlegroups.com
Yes of course. You are right Vikram.

The launched chid/sub process's streams (output,error) need to be separately handled and after which a destroy operation can be attempted.

I guess /K is to just wait until the associated subprocess has done its task. The same can be associated with waitFor() method inside a Java Program. Guess goes this way!


Cheers,
Raghavan alias Saravanan M.
http://groups.google.com/group/S_H_a_D_E

"If at all a single word could efface all the atrocities, I would dare to let my egotism grow and mutilate the people by cozening the belief  first and ask a concomitant SORRY later!" - Raghavan alias Saravanan M.


Raghavan.30May1981_GMail

unread,
Jun 9, 2009, 7:41:17 AM6/9/09
to bo...@googlegroups.com
Sriram, as you said, calling destroy() method eventually does the same. It calls the method on the process instance which holds the subprocess and it kills/destroys both of them.

Nevertheless, Swing GUI also has to be invoked on the underlying platform using which only you would be launching the subprocess. What difference it would have made?


Cheers,
Raghavan alias Saravanan M.
http://groups.google.com/group/S_H_a_D_E

"If at all a single word could efface all the atrocities, I would dare to let my egotism grow and mutilate the people by cozening the belief  first and ask a concomitant SORRY later!" - Raghavan alias Saravanan M.


Sriram Narayanan

unread,
Jun 9, 2009, 7:53:15 AM6/9/09
to bo...@googlegroups.com
On Tue, Jun 9, 2009 at 5:11 PM,
Raghavan.30May1981_GMail<raghavan....@gmail.com> wrote:
> Sriram, as you said, calling destroy() method eventually does the same. It
> calls the method on the process instance which holds the subprocess and it
> kills/destroys both of them.

This destruction will not be guaranteed to happen on time with the
streams closing, and the process destruction using Process itself is
not guaranteed at all, unfortunately.

>
> Nevertheless, Swing GUI also has to be invoked on the underlying platform
> using which only you would be launching the subprocess. What difference it
> would have made?

I started of with invoking a sub-process which was fine with tomcat
3.3. With Tomcat 4, I ended up assembling components together myself
(no server.xml, for e.g.), and learned enough to rewrite Tomcat twice
using just the interfaces. There was no subprocess to invoke.

The other link that I gave to you does a better job than process.
Reply all
Reply to author
Forward
0 new messages