检查进程中是否存在进程 [英] Check if a process exists in go way

查看:131
本文介绍了检查进程中是否存在进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个进程的PID,那么 os.FindProcess 足以测试现有的过程?我的意思是,如果它返回 err ,我可以假设它已被终止(或被杀害)吗?

编辑:



我刚刚写了一个包装函数,它围绕 kill -s 0 (旧式bash过程测试)。这可以毫无问题地工作,但如果有其他解决方案(使用go库)完成这个问题,我仍然很高兴。:

  func checkPid(pid int)bool {
out,err:= exec.Command(kill,-s,0,strconv.Itoa(pid))。CombinedOutput()
如果错误!= nil {
log.Println(err)
}

if string(out)=={
return true // pid exists
}
return false
}


解决方案这是传统的unix方法来查看一个进程是否还活着 - 发送一个0的信号(就像你用bash例子做的那样)。



kill(2)


 如果sig为0,那么不发送信号,但是错误检查仍然是每形成一个b $ b;这可以用来检查是否存在进程ID或
进程组ID。


并翻译成Go

 包主

导入(
fmt
记录
os
strconv
系统调用


func main(){
for _,p:= range os.Args [1:] {
pid,err:= strconv.ParseInt(p,10,64)
if err!= nil {
log.Fatal(err)
}
process,err := os.FindProcess(int(pid))
if err!= nil {
fmt.Printf(找不到进程:%s \ n,err)
} else {
err:= process.Signal(syscall.Signal(0))
fmt.Printf(process.Signal on pid%d returned:%v \ n,pid,err)



$ b $ / code>

当你运行它时你得到这个,显示进程123已经死了,进程1已经存在但不是你所拥有,进程12606还活着并且由你拥有。

  $。/技能1 $ $ 123 
process.Signal on pid 1 returned:操作不允许
process.Signal on pid 12606 returned:< nil>
process.Signal on pid 123 returned:no such process


If I have the PID of a process, is os.FindProcess enough to test for the existing of the process? I mean if it returns err can I assume that it's terminated (or killed)?

Edit:

I've just wrote a wrapper function around kill -s 0 (old-style bash process testing). This works without any problem, but I'm still happy if there is other solutions (done with go libraries) to this problem.:

func checkPid(pid int) bool {
    out, err := exec.Command("kill", "-s", "0", strconv.Itoa(pid)).CombinedOutput()
    if err != nil {
        log.Println(err)
    }

    if string(out) == "" {
        return true // pid exist
    }
    return false
}

解决方案

Here is the traditional unix way to see if a process is alive - send it a signal of 0 (like you did with your bash example).

From kill(2):

   If  sig  is 0, then no signal is sent, but error checking is still per‐
   formed; this can be used to check for the existence of a process ID  or
   process group ID.

And translated into Go

package main

import (
    "fmt"
    "log"
    "os"
    "strconv"
    "syscall"
)

func main() {
    for _, p := range os.Args[1:] {
        pid, err := strconv.ParseInt(p, 10, 64)
        if err != nil {
            log.Fatal(err)
        }
        process, err := os.FindProcess(int(pid))
        if err != nil {
            fmt.Printf("Failed to find process: %s\n", err)
        } else {
            err := process.Signal(syscall.Signal(0))
            fmt.Printf("process.Signal on pid %d returned: %v\n", pid, err)
        }

    }
}

When you run it you get this, showing that process 123 is dead, process 1 is alive but not owned by you and process 12606 is alive and owned by you.

$ ./kill 1 $$ 123
process.Signal on pid 1 returned: operation not permitted
process.Signal on pid 12606 returned: <nil>
process.Signal on pid 123 returned: no such process

这篇关于检查进程中是否存在进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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