title: Haskell read from process
body: <p>I'm not quite sure where the problem is, so I'm uncertain if the title is really accurate. I want to open a shell process, and write to it then read the output:</p>
  
  <pre><code>import System.IO (hPutStr, hGetContents, hSetBuffering, hClose,
              BufferMode(NoBuffering))
  import System.Process (runInteractiveProcess)
  
  main = do
          (hIn, hOut, _, _) &lt;- runInteractiveProcess
                                  "/bin/sh" [] Nothing Nothing
          hSetBuffering hIn NoBuffering
          hPutStr hIn "ls /\n"
          hGetContents hOut &gt;&gt;= putStrLn
          hClose hIn
          hClose hOut
  </code></pre>
  
  <p>This seems to work (sort of):</p>
  
  <pre><code>$ ./mwe
  bin
  boot
  cdrom
  dev
  etc
  ...
  </code></pre>
  
  <p>The problem is, the program hangs (I need to kill it with Ctrl-C). I suppose the shell is still running, which prevents the Haskell prohramfrom exiting. So I tried to terminate the shell explicitly with <code>terminateProcess</code>:</p>
  
  <pre><code>main = do
          (hIn, hOut, _, sh) &lt;- runInteractiveProcess
                                  "/bin/sh" [] Nothing Nothing
          hSetBuffering hIn NoBuffering
          hPutStr hIn "ls /\n"
          hGetContents hOut &gt;&gt;= putStrLn
          terminateProcess sh
          hClose hIn
          hClose hOut
  </code></pre>
  
  <p>but to no avail. I also tried to send <code>exit</code> to the shell:</p>
  
  <pre><code>    ...
      hGetContents hOut &gt;&gt;= putStrLn
      hPutStr hIn "exit\n"
      ...
  </code></pre>
  
  <p>which does not work either. </p>
  
  <p>I'm sure the solution is embarrassingly simple, but I couldn't find it (tried searching for "haskell kill process", among others , but perhaps the problem is not what I think). Apologies in advance if this has been asked already. Any help much appreciated!</p>