How to using createProcess
in haskell redirect stderr into /dev/null
?
Asked
Active
Viewed 440 times
2 Answers
3
Open /dev/null
with withFile
and set it as std_err
in the CreateProcess
record:
import System.Process
import System.IO
main :: IO ()
main = withFile "/dev/null" WriteMode (\handle -> do
(_,_,_,phandle) <- createProcess (shell "echo foo"){ std_err = UseHandle handle}
waitForProcess phandle
return ())
In Windows, you can use the NUL
file.
2
Not the exact solution you are looking for but one popular solution in the community is to use the silently
package to achieve it.
You can use the function hSilence to do want you want. In fact, internally it opens /dev/null
and does all the work. Sample code demo:
#!/usr/bin/env stack
{- stack
--resolver lts-6.15
--install-ghc
runghc
--package process
--package silently
-}
import System.Process
import System.IO
import System.IO.Silently
writeToStderr :: IO ()
writeToStderr = do
hPutStrLn stderr "hello"
hPutStrLn stderr "world"
main :: IO ()
main = hSilence [stderr] writeToStderr

Sibi
- 47,472
- 16
- 95
- 163