Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to terminate (SIGINT) a subprocess in Haskell?

I'm writing a small program in Haskell which manipulates the commands arecordmidi and aplaymidi to record short improvisations on my digital piano through MIDI. I will press the R key, my program will create a new subprocess with the command arecordmidi. When I press R again, I want my recording to stop, by terminating the command arecordmidi.

How do I terminate the arecordmidi subprocess? If in a shell, CTRL+C would stop recording. This is what I want.

I'm using the following code to create the subprocess:

import System.Process

main = do
  let rec_command = "arecordmidi -p \"CASIO USB-MIDI\" myRecording.midi"
  process <- createProcess (shell rec_command)

  -- I could try the following code, but the documentation of System.Process 
  -- says it's bad to use terminateProcess:
  let (_, _, _, processHandle) = process
  terminateProcess processHandle
like image 269
CamilB Avatar asked Dec 28 '16 18:12

CamilB


1 Answers

terminateProcess sends a SIGTERM (terminate) signal to the process, which corresponds to the default behavior of the unix command kill, which generally is not what you want when trying to end a process nicely.

Ctrl+C sends the signal SIGINT (interrupt), which many applications handle by an orderly shutdown, and in your case probably results in the arecordmidi process saving outstanding data, and closing any pipes and files.

Looks like the way to send SIGINT with System.Process is with interruptProcessGroupOf.

like image 133
E4z9 Avatar answered Oct 10 '22 02:10

E4z9