我想使用 Golang for Linux os 從特定子進程 id (pid) 獲取父進程 id (ppid)我有這段代碼,它提供當前進程的 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) }有沒有辦法像這樣傳遞 pidos.Getppid(pid)或任何其他方法來檢索 Golang 中指定 pid 的 ppid ?
2 回答

慕娘9325324
TA貢獻1783條經驗 獲得超4個贊
我認為 go 標準庫不允許您執行此操作,但是, mitchellh/go-ps等第三方軟件包提供了更多信息。
例子:
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())
}
輸出:
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
您還可以用來ps.FindProcess(<pid>)查找特定進程并檢查其 PPid
- 2 回答
- 0 關注
- 505 瀏覽
添加回答
舉報
0/150
提交
取消