bunduru
bunduru

Reputation: 121

psexec stalls when ran with Java

I'm using psexec and sc query state= all to print out all of the services on a remote server. I'm wanting to parse the output of this and have been trying to use a BufferedReader to do this.

Runtime rt = Runtime.getRuntime();
String line = null;
Process pr = null;

pr = rt.exec("test.bat");

BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));

while((line = input.readLine()) != null) {
    System.out.println(line);
}

test.bat

 psexec \\server -u username -p password sc query state= all

When I run psexec \\server -u username -p password sc query state= all from the command line window, I get this;

SERVICE_NAME: Tomcat6 
    TYPE               : 10  WIN32_OWN_PROCESS  
    STATE              : 4  RUNNING 
    WIN32_EXIT_CODE    : 0  (0x0)
    SERVICE_EXIT_CODE  : 0  (0x0)
    CHECKPOINT         : 0x0
    WAIT_HINT          : 0x0

SERVICE_NAME: ActiveMQ 
    TYPE               : 10  WIN32_OWN_PROCESS  
    STATE              : 4  RUNNING 
    WIN32_EXIT_CODE    : 0  (0x0)
    SERVICE_EXIT_CODE  : 0  (0x0)
    CHECKPOINT         : 0x0
    WAIT_HINT          : 0x0

etc.

But in Java, it prints the first service and then stops, so the print out is this;

SERVICE_NAME: Tomcat6 
    TYPE               : 10  WIN32_OWN_PROCESS  
    STATE              : 4  RUNNING 
    WIN32_EXIT_CODE    : 0  (0x0)
    SERVICE_EXIT_CODE  : 0  (0x0)
    CHECKPOINT         : 0x0
    WAIT_HINT          : 0x0

Upvotes: 1

Views: 451

Answers (2)

bunduru
bunduru

Reputation: 121

There seems to be an issue in using psexec with Java. I switched to paexec and everything worked fine.

Upvotes: 1

Carlos Landeras
Carlos Landeras

Reputation: 11063

Have you tried using waitFor?. Maybe the program is exiting without waiting for process termination. Try using:

pr = rt.exec("test.bat");
pr.waitFor();

Upvotes: 0

Related Questions