Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-11-07
8
Medium Priority
?
987 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.
Suggested Courses

618 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