Skip to content

Instantly share code, notes, and snippets.

@seanf
Created August 23, 2017 02:12
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save seanf/58b76e278f4b7ec0a2920d8e5870eed6 to your computer and use it in GitHub Desktop.
Save seanf/58b76e278f4b7ec0a2920d8e5870eed6 to your computer and use it in GitHub Desktop.
Execute process from Kotlin
import java.lang.ProcessBuilder.Redirect
import java.util.concurrent.TimeUnit
fun String.runCommand(workingDir: File? = null) {
val process = ProcessBuilder(*split(" ").toTypedArray())
.directory(workingDir)
.redirectOutput(Redirect.INHERIT)
.redirectError(Redirect.INHERIT)
.start()
if (!process.waitFor(10, TimeUnit.SECONDS)) {
process.destroy()
throw RuntimeException("execution timed out: $this")
}
if (process.exitValue() != 0) {
throw RuntimeException("execution failed with code ${process.exitValue()}: $this")
}
}
@jackmahoney
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment