[Haskell-cafe] Getting PID of a child process

Jason Dusek jason.dusek at gmail.com
Fri Oct 19 21:08:52 CEST 2012


2012/10/19 Donn Cave <donn at avvanta.com>:
> 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.

It seems wrong that in the definition of ProcessHandle__, the PID is not
recoverable once the process has exited. I wonder if this has something
to do with Windows compatibility.

--
Jason Dusek
pgp // solidsnack // C1EBC57DC55144F35460C8DF1FD4C6C1FED18A2B



More information about the Haskell-Cafe mailing list