Jason Dusek | 18 Oct 23:03 2012
Picon

Getting PID of a child process

Hi All,

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?

--
Jason Dusek
pgp // solidsnack // C1EBC57DC55144F35460C8DF1FD4C6C1FED18A2B
Gwern Branwen | 19 Oct 00:38 2012
Picon

Re: Getting PID of a child process

On Thu, Oct 18, 2012 at 5:03 PM, Jason Dusek <jason.dusek <at> 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 * 700000)
                     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:

(Continue reading)

Donn Cave | 19 Oct 02:30 2012

Re: Getting PID of a child process

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 <at> (OpenHandle t) = return (h, Just t)
getPID h <at> (ClosedHandle t) = return (h, Nothing)

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

(Continue reading)

Jason Dusek | 19 Oct 21:08 2012
Picon

Re: Getting PID of a child process

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 <at> (OpenHandle t) = return (h, Just t)
> getPID h <at> (ClosedHandle t) = return (h, Nothing)
>
> main = do
>         (h0, h1, h2, hp) <- runInteractiveProcess "/bin/date" [] Nothing Nothing
(Continue reading)


Gmane