Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9071779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:59:55+00:00 2026-06-16T17:59:55+00:00

I want to invoke a process from within a haskell program and capture stdout

  • 0

I want to invoke a process from within a haskell program and capture stdout as well as stderr.

What I do:

(_, stdout, stderr) <- readProcessWithExitCode "command" [] ""

The problem: This way, stdout and stderr are captured separately, however I want the messages to appear in the right place (otherwise I would simply stdout ++ stderr which separates error messages from their stdout counterparts).

I do know that I could achieve this if I’d pipe the output into a file, i.e.

tmp <- openFile "temp.file" ...
createProcess (proc "command" []) { stdout = UseHandle tmp,
                                    stderr = UseHandle tmp }

So my current workaround is to pipe outputs to a tempfile and read it back in. However I’m looking for a more direct approach.

If I was on unix for sure I’d simply invoke a shell command á la

command 2>&1

and that’s it. However, I’d like to have this as portable as possible.

What I need this for: I’ve built a tiny haskell cgi script (just to play with it) which invokes a certain program and prints the output. I want to html-escape the output, thus I can’t simply pipe it to stdout.

I was thinking: Maybe it’s possible to create an in-memory-handle, like a PipedInputStream/PipedOutputStream in Java, or ArrayInputStream/ArrayOutputStream which allows for processing IO streams within memory. I looked around for a function :: Handle on hoogle, but did not find anything.

Maybe there is another Haskell module out there which allows me to merge two streams?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T17:59:59+00:00Added an answer on June 16, 2026 at 5:59 pm

    You can use pipes to concurrently merge two input streams. The first trick is to read from two streams concurrently, which you can do using the stm package:

    import Control.Applicative
    import Control.Proxy
    import Control.Concurrent
    import Control.Concurrent.STM
    import System.Process
    
    toTMVarC :: (Proxy p) => TMVar a -> () -> Consumer p a IO r
    toTMVarC tmvar () = runIdentityP $ forever $ do
        a <- request ()
        lift $ atomically $ putTMVar tmvar a
    
    fromTMVarS :: (Proxy p) => TMVar a -> () -> Producer p a IO r
    fromTMVarS tmvar () = runIdentityP $ forever $ do
        a <- lift $ atomically $ takeTMVar tmvar
        respond a
    

    I will soon provide the above primitives in a pipes-stm package, but use the above for now.

    Then you just feed each Handle to a separate MVar and read from both concurrently:

    main = do
        (_, mStdout, mStderr, _) <- createProcess (proc "ls" [])
        case (,) <$> mStdout <*> mStderr of
            Nothing               -> return ()
            Just (stdout, stderr) -> do
                out <- newEmptyTMVarIO
                err <- newEmptyTMVarIO
                forkIO $ runProxy $ hGetLineS stdout >-> toTMVarC out
                forkIO $ runProxy $ hGetLineS stderr >-> toTMVarC err
                let combine () = runIdentityP $ forever $ do
                        str <- lift $ atomically $
                            takeTMVar out `orElse` takeTMVar err
                        respond str
                runProxy $ combine >-> putStrLnD
    

    Just change out putStrLnD with however you want to process the input.

    To learn more about the pipes package, just read Control.Proxy.Tutorial.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to invoke my rake task from console. Is it doable? if yes,
This is my javascript method in .aspx file. I want to invoke this method
I need to invoke a process which doesn't require any input from the user,
I am using the slider control from WebFX . I want to invoke a
I want to use a 32-bit in-proc COM server from a 64-bit process on
I want to invoke an external GUI application from a python script which will
How can I start a Java application from within a C++ process? I don't
I want to change a Form control property from a process thread event fire
I want to invoke the method by the method name stored in the list.
I want to invoke a selector of a method that has the usual NSError**

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.