【问题标题】:Kill a hanging Process from Scala从 Scala 中杀死一个挂起的进程
【发布时间】:2011-04-21 20:08:02
【问题描述】:

我的代码必须调用一些有时会挂起的外部程序。 (无限循环,永远不会回来)

要启动我使用的外部进程:

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

但我没有找到检查进程是否仍在运行的方法。或 waitFor(time) 以便我只等待一段时间。

我相信他们应该是一个简单的解决方案,但我找不到它......

【问题讨论】:

    标签: scala process


    【解决方案1】:

    据我所知,Process 中的方法 exitValue 定义如下:

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

    因此您可以检查exitValue() 是否返回NoneSome 值。 None 表示该进程仍在运行。它来自documentation to Java Process.exitValue()

    【讨论】:

      猜你喜欢
      • 2012-03-12
      • 2012-01-31
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多