Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the process ID of a created process in Haskell?

Tags:

haskell

Maybe I'm just missing something obvious in the System.Process API (http://hackage.haskell.org/package/process), but it doesn't appear to support getting the raw PID of a process created. The API usually returns a ProcessHandle which can be used easily enough, but this doesn't appear to fulfill a deployment need I have.

I have a case where I want to spawn a long-running process, log the PID it's using, and be able to automatically come back at a later time (days, weeks, months) and kill the old process and re-start with a new process. I'm sure there are several ways to do this auto-deploy-and-restart, but PIDs seemed like the simplest way to do so without too much platform-dependent code.

I'm open to other suggestions about my underlying problem, but it seems odd to me that I can't find any direct PID references (or a way to convert to them) in the process API. This seems like an oversight of the API.

like image 455
stormont Avatar asked Dec 09 '14 20:12

stormont


1 Answers

Here is some example 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 haven't tested this under Windows, but it works on OSX and, presumably, Linux.

For Windows, the Win32 package has a getProcessId function in the module System.Win32.Process, and according to code I've 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 am basing this on is the code for interruptProcessGroupOf (link)

like image 139
ErikR Avatar answered Nov 09 '22 00:11

ErikR