2

My Scala script creates subprocesses like this:

val exitValue = Process(Seq("bash", "-c", command), dir) ! processLogger

with command being e.g. "mvn clean package" or for testing of this issue "sleep 20".

For some reason my script has to intercept SIGINT, because the user might have meant “copy” instead of “stop it” (really happens from time to time). I achieved this by adding a signal handler like this:

Signal.handle(new Signal("INT"), this)

override def handle(sig: Signal)
{
    log.warn("Ignoring SIGINT (Ctrl+C). Press [Esc] instead.")
}

However when a subprocess is running this does not work, because CTRL+C makes it stop. What can I do so that the subprocess ignores SIGINT? I found a solution for Python: Python: How to prevent subprocesses from receiving CTRL-C / Control-C / SIGINT Is there something similar for Scala?

Marcus
  • 1,857
  • 4
  • 22
  • 44

1 Answers1

1

Since you launch bash, you may use trap built-in command to ignore signals in a subprocess.

Just prepend trap '' SIGINT; (with the semicolon) to the command:

Process(Seq("bash", "-c", "trap '' SIGINT;" + command))
apangin
  • 92,924
  • 10
  • 193
  • 247
  • Thanks a lot! I even thought of calling `python` to achieve this – Marcus Jan 26 '17 at 07:58
  • Actually you might want to take a look at this neat trick to run the process in different process group so that a user CTRL-C is not dispatched to your sub-process(es): https://stackoverflow.com/a/51256203/926540 – 2072 Jul 26 '20 at 14:25