Here is a sample code:
import System.Process import System.Process.Internals -- | returns Just pid or Nothing if process has already exited getPid ph = withProcessHandle ph go where go ph_ = case ph_ of OpenHandle x -> return $ Just x ClosedHandle _ -> return Nothing main = do (_,_,_,ph) <- createProcess $ shell "echo $$" getPid ph >>= print
Note. I have not tested this under Windows, but it works on OSX and, presumably, on Linux.
For Windows, the Win32 package has the getProcessId
function in the System.Win32.Process
module, and according to the code I read, this should work:
import System.Win32.Process (getProcessId) main = do (_,_,_,ph) <- createProcess $ shell "echo $$" pid <- withProcessHandle ph go print pid where go (OpenHandle x) = fmap Just $ getProcessId x go (ClosedHandle _) = return Nothing
The code I'm building on this is the code for interruptProcessGroupOf
(link)
source share