My code has to call some external programs which sometimes hangs. (endless loop, will never return)
To start the external Process i use:
import tools.nsc.io.Process
val res = Process("ls")
res.foreach(println)
res.waitFor // waits until a Process is finished but if it's hanging waitFor wi开发者_如何学运维ll not return or
res.destroy // kills a process
But i didn't find a way to check if the process is still running. Or a waitFor(time) so that i only wait for some time.
I believe their should be a simple solution but i'm not able to find it...
As far as I can see method exitValue
in Process
is defined as folows:
def exitValue(): Option[Int] =
catching(classOf[IllegalThreadStateException]) opt process.exitValue()
So you can check whether exitValue()
returns None
or Some
value. None
means that process is still running. It follows from documentation to Java Process.exitValue()
精彩评论