Solved

Runtine.exec("..") hangs with PSKill Command

Posted on 2006-11-07
8
957 Views
Last Modified: 2008-01-09
I am unable to get his function of my script to work. It tries to launch a process to kill another process, but it hangs at the waitfor() command. I have tried processing the input and error streams yet it continues to happen. Any ideas?

Here is the call to runCommand function

runCommand("pskill -t \\\\" + computer + " -u username -p password ibmdsa2");


It here is the function

public int runCommand(String aStr)
{
try{
Process proc = Runtime.getRuntime().exec(aStr);
InputStream stdin = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null){System.out.println("here");}
int exitVal = proc.waitFor();
System.out.println("done");
return  exitVal;
} catch (Exception e){System.out.println(e);return -1;}

}

It doesn't reach the "done" line.
0
Comment
Question by:bbcac
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17892073
try treating the streams on separate threads. See

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
0
 

Author Comment

by:bbcac
ID: 17892279
That looks to be working...thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17892415
No problem
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bbcac
ID: 17893060
Ok... it seems to get farther in the process but it is not hanging on another command.

I used the Runtime.exec('') to send a command to run a tool on remote computer. The tool, if run from the command line, takes about 5-10 minutes to complete. Even when you handle the input nad error streams seperatly, it seems to hang in the thread @ the following line:

while ( (line = br.readLine()) != null){System.out.println(line);}  //used to read the streams for input and error

Keep in mind that the tool does run successfully on the remote system but it doesn't every destroy the process on the remote system. On this end, it just sits there waiting for more input and/or errors.

Here's the relavent code:

//The call the run the remote process
runCommand("pskill -t \\\\" + computer + " -u username -p password ibmdsa2");


//the function to handle the command run
public int runCommand(String aStr)
{
try{
Process proc = Runtime.getRuntime().exec(aStr);
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new       StreamGobbler(proc.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();

// any error???
int exitVal = proc.waitFor();

return exitVal;
}
catch(Exception e){System.out.println(computer + " error occured: " + e);return -1;}

}


//finally this is the thread class that hands the streams (input and error)
import java.util.*;
import java.io.*;
class StreamGobbler extends Thread
{
    InputStream is;
    String type;

    StreamGobbler(InputStream is, String type)
    {
        this.is = is;
        this.type = type;
    }

    public void run()
    {
        try
        {
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            String line=null;
            while ( (line = br.readLine()) != null){System.out.println(line);}
            System.out.println("I'm out");
            } catch (IOException ioe)
              {
                ioe.printStackTrace();
              }
    }
}
0
 

Author Comment

by:bbcac
ID: 17893361
sorry the call to thie function is as follows

System.out.println(computer + " status: " + runCommand("psexec \\\\" + computer + " -u admin -p psswd -c -v ibmdsa2.exe -c -d \"" + path + computer + "\""));
0
 

Author Comment

by:bbcac
ID: 17965920
The issue has been solved through a work around, I no longer need help with this. Thanks,
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 17966182
OK that's good. Please close the question then
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17980524
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
login form jsp example 2 50
jsp login check 12 53
Java basic valueOf question 1 33
glassfish admin console 1 21
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question