6

I am currently using the scala process API scala.sys.processbut, I can't seem to figure out how to gracefully terminate a process that react to the SIGTERM signal. I've done it in python before where there are pretty terminate and send_signal function on the process, but on the scala scala.sys.process.Process object, all I see is destroy. To me it look like scala will nuke my process from orbit, just to be sure, and thats not what I want.

How can I tell this process that it should clean itself and exit from my scala code?

val launcher = Process("myprocess", Seq("args"))
val process = launcher.run()
process.destroy() //Err... no? terminate or equivalent like in python please?

EDIT

For more detail : My scala process is launching a C++ subprocess, that listen to signal handler (SIGTERM, SIGKILL and the like) to know when to exit. It has been well tested and it clean itself correctly. My problem is that I don't know how to send that signal from my scala application! Thus, my C++ process always get dragged outside and shot instead of just being asked to stop.

Laurent Bourgault-Roy
  • 2,774
  • 1
  • 30
  • 36
  • Are you positive `.destroy()` results in SIGKILL? For my external processes it seems to result in SIGTERM – Hamy Sep 19 '14 at 16:41
  • Well the javadocs of destroy (on which scala is based) says : "Kills the subprocess. The subprocess represented by this Process object is **forcibly terminated**" (emphasis mine) Most other language I know have two method : kill and terminate (or the ability to specify the signal). But maybe internally, on some platform, Java use SIGTERM. There is no way to be sure :( – Laurent Bourgault-Roy Sep 19 '14 at 17:57
  • on Linux, my processes definitely get SIGTERM (just checked). In fact, I can't find a way to send a SIGKILL from Scala/Java directly, I have to open another subprocess and kill -9 :-( This is corroborated by http://stackoverflow.com/questions/10630303/java-process-destroy-source-code-for-linux – Hamy Sep 19 '14 at 18:09
  • That language is confusing though. Not all platforms have graceful vs non-graceful external process termination, so perhaps they just want you to be prepared for the worst when using the destroy method – Hamy Sep 19 '14 at 18:10
  • Indeed. I think on windows the termination is the equivalent of sigkill (I was doing multiplatform code when I originally wrote that question). Thankfully, [there is an API update scheduled for Java 9](http://openjdk.java.net/jeps/102). Maybe in two year, if we're lucky, we'll be able to know how to shutdown properly a process in Java/scala :P – Laurent Bourgault-Roy Sep 19 '14 at 19:04
  • Oh I'm so glad to see this addition in the pipeline. Tired of only getting the smallest common feature set and having horrid support for advanced features on one platform, I'd rather at least have some advanced APIs for nice platform-specific support if you want to use it. So tired of parsing `ps` output all the time on linux-only hosts ;-) – Hamy Sep 19 '14 at 19:58

1 Answers1

3

Scala Process stuff is based on Java's, and, therefore, subject to the same limitations. Java provides a very poor interface. Perhaps it makes it possible to use the same interface on more systems, but it is incredibly limiting to those working on Posix systems.

Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681