나는 스칼라 프로그램 내에서 시작되는 장기 실행 외부 프로세스를 가지고 있으며 스칼라 프로세스가 종료되거나 죽을 때 프로세스를 종료하려고합니다. 이것을 달성하는 가장 간단한 방법은 무엇입니까?scala 프로세스가 종료 될 때 sys.process를 종료 하시겠습니까?
참고로는 여기에 몇 가지 코드 (ps aux | grep [a]vahi
도 Main
후 종료를 실행하여 볼 때 avahi-publish
여전히 실행되므로주의)이다 :
object Main extends App {
Executors.newSingleThreadScheduledExecutor.submit(
new Runnable {
override def run(): Unit = {
while (true) {
println("starting avahi publish")
Try {
val cmd = "avahi-publish-service 'aname' '_foobar._tcp' 1234"
Seq("sh", "-c", cmd).!
} match {
case Success(r) => println(s"avahi publish terminated: $r")
case Failure(e) => println(s"avahi publish error: $e")
}
}
}
})
// etc etc
}
java에서 비슷한 질문을 보려면 https://stackoverflow.com/questions/269494/how-can-i-cause-a-child-process-to-exit-when-the-parent-does를 참조하십시오. 동일한 대답이 여기에 적용되어야합니다. – gourlaysama