Fabian
Fabian

Reputation: 1224

Kill a hanging Process from Scala

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 will 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...

Upvotes: 4

Views: 1517

Answers (1)

tonek
tonek

Reputation: 403

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()

Upvotes: 3

Related Questions