Kill the suspension process with Scala

My code should call some external programs that sometimes freeze. (endless loop, never coming back)

To start an 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 hanging waitFor will not return or res.destroy // kills a process 

But I did not find a way to check if the process continues. Or waitFor (time), so I'm only waiting for a while.

I believe that they should be a simple solution, but I cannot find it ...

+4
source share
1 answer

As far as I can see, the exitValue method in Process is defined as the following:

 def exitValue(): Option[Int] = catching(classOf[IllegalThreadStateException]) opt process.exitValue() 

So you can check if exitValue() returns None or Some . None means the process is still running. From Java documentation Process.exitValue()

+3
source

All Articles