[英]Get Parent process id from child process id using Golang
I want to get parent process id (ppid) from specific child process id (pid) using Golang for Linux os 我想使用Golang for Linux os从特定的子进程ID(pid)获取父进程ID(ppid)
I have this code which gives ppid and pid of the current process but I want to retrieve ppid of the child process which I specify and not the current process. 我有这段代码给出了当前进程的ppid和pid,但是我想检索指定的子进程的ppid而不是当前进程。
package main
import (
"fmt"
"os"
)
func main() {
pid := os.Getpid()
parentpid := os.Getppid()
fmt.Printf("The parent process id of %v is %v\n", pid, parentpid)
}
Is there a way to pass pid like this os.Getppid(pid)
or any other method to retrieve ppid of specified pid in Golang? 有没有办法像os.Getppid(pid)
这样传递pid或任何其他方法来检索Golang中指定pid的ppid?
I don't think the go standard library allows you to do this, however, third-party packages such as mitchellh/go-ps provide more information. 我认为go标准库不允许您执行此操作,但是mitchellh / go-ps等第三方程序包可提供更多信息。
Example: 例:
import ps "github.com/mitchellh/go-ps"
...
list, err := ps.Processes()
if err != nil {
panic(err)
}
for _, p := range list {
log.Printf("Process %s with PID %d and PPID %d", p.Executable(), p.Pid(), p.PPid())
}
Output: 输出:
2019/06/12 09:13:04 Process com.apple.photom with PID 68663 and PPID 1
2019/06/12 09:13:04 Process CompileDaemon with PID 49896 and PPID 49895
You can also use ps.FindProcess(<pid>)
to find a specific process and inspect its PPid 您还可以使用ps.FindProcess(<pid>)
查找特定进程并检查其PPid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.