Re: [Haskell-cafe] Getting PID of a child process
2012/10/19 Donn Cave d...@avvanta.com: Quoth Jason Dusek jason.du...@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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Getting PID of a child process
On Thu, Oct 18, 2012 at 5:03 PM, Jason Dusek jason.du...@gmail.com wrote: For diagnostic purposes, I'd like to print the PID of the process attached to this handle -- how best to do that? In Mueval when I wanted the PID (so I could later send sigkills), I did this: hdl - runProcess mueval-core args Nothing Nothing Nothing Nothing Nothing _ - forkIO $ do threadDelay (7 * 70) status - getProcessExitCode hdl case status of Nothing - do terminateProcess hdl _ - withProcessHandle hdl (\x - case x of OpenHandle pid - signalProcess 9 pid return (undefined, undefined) _ - return (undefined,undefined)) exitWith (ExitFailure 1) Just a - exitWith a stat - waitForProcess hdl exitWith stat The key is the poorly documented withProcessHandle :: System.Process.Internals.ProcessHandle - (ProcessHandle__ - IO (ProcessHandle__, a)) - IO a The implementation: data ProcessHandle__ = OpenHandle PHANDLE | ClosedHandle ExitCode type PHANDLE = CPid Well, my code seems to work, anyway... -- gwern http://www.gwern.net ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Getting PID of a child process
Quoth Jason Dusek jason.du...@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 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe