是的,Golang 中有第三方库可以实现枚举进程并获取所有进程的全路径。其中比较常用的库是 github.com/mitchellh/go-ps。
该库提供了一个 Processes() 函数,可以返回当前系统中所有进程的列表。每个进程都包含 PID、父进程 PID、命令行参数等信息。然后可以遍历这个列表,使用 os/exec 包中的 Command() 函数来执行 which 命令获取每个进程的全路径。
下面是一个简单的示例代码:
package main
import (
"fmt"
"os/exec"
"github.com/mitchellh/go-ps"
)
func main() {
processes, err := ps.Processes()
if err != nil {
fmt.Println(err)
return
}
for _, process := range processes {
cmd := exec.Command("sh", "-c", fmt.Sprintf("which %s", process.Executable()))
output, err := cmd.Output()
if err != nil {
continue
}
fullPath := string(output)
fmt.Printf("Process: %d, Full Path: %s\n", process.Pid(), fullPath)
}
}
上述代码会输出系统中所有进程的 PID 和全路径信息。需要注意,在 Linux 系统中,某些进程可能由内核启动或者没有对应的可执行文件,此时可能无法获取到其全路径信息。




