Merge pull request #1 from Nohus/execute_hang

Fixed hang in execute() call
master
Robinson 2023-11-19 21:01:29 +01:00 committed by GitHub
commit da300e87dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 3 deletions

View File

@ -438,12 +438,13 @@ object OS {
* This is based on an aggregate of the answers provided here: [https://stackoverflow.com/questions/35421699/how-to-invoke-external-command-from-within-kotlin-code]
*/
private fun execute(vararg args: String, timeout: Long = 60): String {
return ProcessBuilder(args.toList())
val process = ProcessBuilder(args.toList())
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectError(ProcessBuilder.Redirect.PIPE)
.start()
.apply { waitFor(timeout, TimeUnit.SECONDS) }
.inputStream.bufferedReader().readText().trim()
val text = process.inputStream.bufferedReader().readText().trim()
process.waitFor(timeout, TimeUnit.SECONDS)
return text
}
// true if the exit code is 0 (meaning standard exit)