I am translating code from a NodeJS server to a Kotlin Ktor server.
The NodeJS code splits the output between a String to be handled by code, and realtime server logging :
const shellScript = exec("./myScript.sh",
(error, stdout, stderr) => {
// this happens when the script ends
// stdout and stderr contain full script output
if (error === null) {
...
}
});
// Realtime server logging
shellScript.stdout.on('data', (data) => { console.log(data); });
shellScript.stderr.on('data', (data) => { console.error(data); });
According to my understanding of java.lang.ProcessBuilder
, we would need to spawn a thread (or a coroutine) to poll inputStream and errorStream in a loop and accumulate them in a volatile field.
Is there a cleaner way to do this?