title: Kill a sys.process when the scala process dies?
body: <p>I have a long-running external process that is started inside a scala program, and I want to have the process die when the scala process dies (or is killed). What's the simplest way to achieve this?</p>
  
  <p>For reference, here's some code (note that <code>avahi-publish</code> will still be executing as seen by running <code>ps aux | grep [a]vahi</code> even after <code>Main</code> terminates):</p>
  
  <pre><code>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) =&gt; println(s"avahi publish terminated: $r")
              case Failure(e) =&gt; println(s"avahi publish error: $e")
            }
          }
        }
      })
    // etc etc
  }
  </code></pre>