Exec starting remote process with psexec does not return (on windows)


(r3dox) #1

I am trying to use exec to start a process on a remote windows machine. I am using the psexec tool from sysinternals.com. The first call which reads the JAVA_HOME environment variable from the remote machine is working. The problem is that the call to ping is never returning. When I execute the same command from the commandline outside of gradle it works without problems.

Any ideas? I am using gradle 1.3 How does the exec task determine when a process has finished?

thanks in advance!

ext.psExecCmd = "C:/tools/sys/PsExec.exe"
ext.serverName = "yourserver"
ext.winUser = "localhost\Administrator"
ext.winUserPw = "yourpw"
  task callping() << {
  def psExecArgs = ["\${project.serverName}", "-user", "${project.winUser}", "-p", "${project.winUserPw}", "cmd", "/c"]
  new ByteArrayOutputStream().withStream { os ->
    def result1 = exec {
      executable = project.psExecCmd
      args = psExecArgs + ["echo", "%JAVA_HOME%", "&&", "exit"]
      standardOutput = os
    }
    def outputAsString = os.toString().trim()
    project.ext.remoteJavaHome = outputAsString
    println "output: $outputAsString"
    println "remoteJavaHome: $project.remoteJavaHome"
    def resultTest = exec {
      executable = project.psExecCmd
      args = psExecArgs + ["ping", "localhost", "&&", "exit"]
    }
    println "callping done!"
  }
}

(Peter Niederwieser) #2

The ‘Exec’ task uses the regular Java ‘java.lang.ProcessBuilder’ API. On Windows you typically have to use ‘cmd /c’ to run a command.


(r3dox) #3

Thanks for your reply.

As you can see i am already using the “cmd /c” command here:

def psExecArgs = ["\${project.serverName}", "-user", "${project.winUser}", "-p", "${project.winUserPw}", "cmd", "/c"]

(Peter Niederwieser) #4

‘cmd’ is the executable, ‘/c’ the first argument, and 'psexec ’ the second argument. Try it on the command line.


(r3dox) #5

Sorry i dont understand what i am supposed to try.

i am using psexec.exe to call the command “cmd /c ping localhost” on a remote machine. the first few lines of the ping output are shown on the console but then there is no more progress and exec/gradle does not return.

if i run gradle in info mode and copy out the used command and run it standalone in a local console it works without problems (and shows the output of the ping command of the remote server).

do you want me to start the psexec on my local machine also with a “cmd /c” in the front? i tried that, the problem was the same, see here:

Starting process 'command 'cmd''. Working directory: C:\_gradle Command: cmd /c C:/tools/sys/PsExec.exe \yourserver -user localhost\Administrator -p yourpw cmd /
c ping localhost && exit
Successfully started process 'command 'cmd''
  PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
    Ping wird ausgef?hrt f?r yourserver [::1]
> Building > :callping

And then no more progress.


(Peter Niederwieser) #6

do you want me to start the psexec on my local machine also with a “cmd /c” in the front?

Yes, that was my intention. Have you checked if the ‘psexec’ process ever terminates when called from Gradle? I guess not.


(r3dox) #7

The psexec process is still running, if i shut it down gradle exits with a return code error ( = 1, as expected ).

Any idea why the process wont terminate when called from gradle but will terminate when called from the console?


(Peter Niederwieser) #8

Not really. It would be good to know if you can get it to work with a plain Java or Groovy command-line app that uses ‘java.lang.ProcessBuilder’ to call ‘psexec’. Also make sure to try with ‘–no-daemon’.


(r3dox) #9

I tried with the “–no-daemon” parameter, still the same problem.

If i have more time i might try with the processbuilder.

But isnt it strange that the first call to the echo command on the remote machine works without problems? I tried to have the ping call being the only exec task, still the same problem.


(r3dox) #10

this seems to be a general problem with psexec:

http://stackoverflow.com/questions/3682984/psexec-from-ant-does-not-terminate http://stackoverflow.com/questions/8681142/running-psexec-from-java-does-not-work

They suggest to use the -d parameter which does not wait for the process to terminate. The return value will then be the pid of the created process.

I was able to make it work without -d using paexec instead of psexec: http://www.poweradmin.com/PAExec/

how to mark this answer as correct?