Solved

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

Posted on 2006-11-07
8
952 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
stackato and cloud 4 87
servlet example issue 6 40
MySqlDump not dumping triggers 1 33
Tomcat: Unable to run tomcat service. 2 20
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

825 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