Created
August 23, 2017 02:12
-
-
Save seanf/58b76e278f4b7ec0a2920d8e5870eed6 to your computer and use it in GitHub Desktop.
Execute process from Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks