如何获得Haskell中创建的进程的进程ID? [英] How to get the process ID of a created process in Haskell?

查看:122
本文介绍了如何获得Haskell中创建的进程的进程ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我只是在System.Process API中丢失了一些明显的东西( http://hackage.haskell.org/package/进程),但它似乎不支持获取创建的进程的原始PID。 API通常会返回一个可以很容易地使用的ProcessHandle,但这似乎并不符合我的部署需求。



我有一个情况,我想要产生一个长期运行的过程,记录它使用的PID,并能够在以后的时间(几天,几周,几个月)自动回来,并杀死旧的过程并重新启动一个新的过程。我确信有几种方法可以实现这种自动部署和重新启动,但是PID似乎是最简单的方法,不需要太多依赖于平台的代码。



<我对其他有关我的潜在问题的建议持开放态度,但对于我来说,在流程API中找不到任何直接PID引用(或转换为它们的方法)似乎很奇怪。这看起来像是对API的监督。

解决方案

以下是一些示例代码:

  import System.Process 
import System.Process.Internals

- 如果进程已退出,则返回Just pid或Nothing
getPid ph = withProcessHandle ph go
where
go ph_ = case ph_
OpenHandle x - >返回$ Just x
ClosedHandle _ - > return Nothing

main = do
(_,_,_,ph)< - createProcess $ shellecho $$
getPid ph>> = print

注意:我没有在Windows下测试过它,但它可以在OSX和Linux上使用。

对于Windows, Win32包在模块 System.Win32.Process 中有一个 getProcessId 函数,根据我的代码阅读,这应该工作:

$ p $ import System.Win32.Process(getProcessId)

main = do
(_,_,_,ph)< - createProcess $ shellecho $$
pid< - withProcessHandle ph go
print pid

where go(OpenHandle x)= fmap只需$ getProcessId x
go(ClosedHandle _)= return Nothing

我基于此的代码是 interruptProcessGroupOf (链接)


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.

解决方案

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)

这篇关于如何获得Haskell中创建的进程的进程ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆