Question

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
Was it helpful?

Solution

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

OTHER TIPS

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

pr = rt.exec("test.bat");
pr.waitFor();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top