[Haskell-cafe] Getting PID of a child process

Donn Cave donn at avvanta.com
Fri Oct 19 02:30:56 CEST 2012


Quoth Jason Dusek <jason.dusek at gmail.com>,

> Using `System.Process.runInteractiveProcess', I can start a process
> and get a handle to it:
> 
>   runInteractiveProcess
>    :: FilePath
>    -> [String]
>    -> Maybe FilePath
>    -> Maybe [(String, String)]
>    -> IO (Handle, Handle, Handle, ProcessHandle)
> 
> For diagnostic purposes, I'd like to print the PID of the
> process attached to this handle -- how best to do that?


There's a good chance this isn't the best way, but it seems to work:


import System.Process
import System.Process.Internals (ProcessHandle__(..), PHANDLE, withProcessHandle)

-- for use with withProcessHandle
getPID :: ProcessHandle__ -> IO (ProcessHandle__, Maybe PHANDLE)
getPID h@(OpenHandle t) = return (h, Just t)
getPID h@(ClosedHandle t) = return (h, Nothing)

main = do
	(h0, h1, h2, hp) <- runInteractiveProcess "/bin/date" [] Nothing Nothing
	mp <- withProcessHandle hp $ getPID
	print mp

Seems like more scaffolding than this application really ought to require.

	Donn





More information about the Haskell-Cafe mailing list