Solved

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

Posted on 2006-11-07
8
942 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
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
That looks to be working...thanks
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
No problem
0
 

Author Comment

by:bbcac
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:bbcac
Comment Utility
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
Comment Utility
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
Comment Utility
OK that's good. Please close the question then
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now