Search code examples
gradlebackground-process

Run a background job from Gradle


I've created a task starting a remote job like

task mytask(type: Exec) {
    commandLine 'ssh'
    args '-f -l me myserver ./start'.split(' ')
}

and it works, however, it seems to wait for the job to terminate. But it never terminates and it shouldn't.

Doing the same from the command line works: Because of the -f switch the ssh command gets executed in the background.

I've tried to add '>&' /dev/null (csh stdout and stderr redirect) to the command line, but without any success. Also the obvious & did nothing. I also extracted the command line into a script, and it's always the same: Gradle waits for termination.

Solution

I've solved it by using a script and redirecting both stdout and stderr in the script. My problem came from confusing redirections... by passing '>&' /dev/null I redirected the streams on the remote computer, but what was needed was a redirection on the local one (i.e., without putting the redirection operator in quotes).


Solution

  • The Exec task always waits for termination. To run a background job, you need to write your own task, which could, for example, use the Java ProcessBuilder API.