aspose file tools*
The moose likes Threads and Synchronization and the fly likes Process.destroy is not working on process invoked by sub-thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Process.destroy is not working on process invoked by sub-thread" Watch "Process.destroy is not working on process invoked by sub-thread" New topic
Author

Process.destroy is not working on process invoked by sub-thread

Lakshya Bhardwaj
Greenhorn

Joined: Nov 28, 2012
Posts: 1
Hi,

Sory I am doing Java code after many many years so might be doing something wrong and need help in resolving this issue.

My main thread (or main program) is reading a file and for each entry in file it is invoking a sub-thread that perform series of action like doing ping etc.

In each child thread I am invoking process to execute certain tasks or execute batch file to make connection to remote server and perform activity.

There are certain server where commands\tasks are not returning and hence compelete program goes into hung state. I want to kill those process that triggered those commands\tasks and trying to use process.destroy. But tt is not working at all.

Please suggest what could be the issue as I have tried almost everything that I can think of and how to fix it. Thank a ton in advance.



public static void main(String [] args) throws InterruptedException
{

'
'
'

Thread thread = new OQ(IP,UserName,Pass,Port,FQDN,OS,dateNow,LoggedInUser,"10.146.31.120:1828","mc");
thread.start();
thread.setName(IP+dateNow);
'
'
'
}



public void run()
{
.
.
.


cmnd = "agent_check.bat " + UserName + " " + Pass + " " + IP + " " + Port + " 40";

Full_Dump += cmnd+"\n";

// cmnd="cmd.exe /C patrolcli \"user " + UserName + " " + Pass + "\" \"connect " + IP + " " + Port + "\" \"execpsl set(\\\"/AS_EVENTSPRING/EVENTSPRING/AlertTest/value\\\",40);\""; i also tried executing command directly instead of wrapping it in batch file
try
{
p=Runtime.getRuntime().exec(cmnd);

BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));

while( (cmdOutput=reader.readLine()) != null )
{
Full_Dump += cmdOutput+"\n";
Output += cmdOutput+"\n";
System.out.println(cmdOutput);
}

System.out.println("reading done");
p.waitFor();

} catch (InterruptedException ie)
{

System.out.println("killing process");
p.waitFor();
Full_Dump +="Interrupted while waiting for process; time likely ran out. Destroying process...";
p.destroy();

}

Thanks
Gaurav
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
But tt is not working at all.


How far does it actually get?



Suppose the output is never terminated with a newline character? (Thats why I just read character by character from the process output)

Bill
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1058
    
  10

There are two obvious things wrong with your code fragments. First, though you call p.waitFor() you totally ignore the return code which may or may not contain diagnostic information and second you ignore the process 'stderr'! The most common 'hang up' scenario I have met when using Runtime.exec() is caused by failure to handle 'stderr' properly when the process fails. In this situation one can get a deadlock when the 'stderr' buffer becomes full and the process suspends until more buffer space is available to write more of 'stderr'. If the Java side does not read the 'stderr' stream the buffer never ever ever has space available - a deadlock!

All this and more is detailed in the ancient but still very relevant article - http://www.javaworld.com/jw-12-2000/jw-1229-traps.html . You should read this and implement all the recommendations. Don't be tempted to miss out anything because you think it cannot apply to you because it can. Note - pay particular attention to the use of separate threads for handling the various Process streams.
 
 
subject: Process.destroy is not working on process invoked by sub-thread