Solved

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

Posted on 2006-11-07
8
947 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
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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 68
micro services spring boot application error 3 139
windows explorer path to command prompt 5 27
arguments to jar 5 7
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

932 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

8 Experts available now in Live!

Get 1:1 Help Now