英文:
List of currently running process in Go
问题
如何在Go中获取当前正在运行的进程列表?
OS包提供了一些函数:http://golang.org/pkg/os/
但是没有提供任何方法来查看正在运行的进程列表。
英文:
How can I get the list of currently running processes in Go?
The OS package provides some functions: http://golang.org/pkg/os/
but doesn't give anything to see the list of running processes.
答案1
得分: 24
在标准库中没有这样的函数,很可能永远也不会有。
在大多数情况下,程序不需要进程列表。Go程序通常只需要等待一个或少数几个进程,而不是所有进程。通常通过其他方式获取进程的PID,而不是搜索所有进程的列表。
如果你在Linux上,可以通过读取<code>/proc</code>目录的内容来获取进程列表。参考问题https://stackoverflow.com/questions/939778/linux-api-to-list-running-processes
英文:
There is no such function in the standard library and most likely never will be.
In most cases, the list of processes isn't required by programs. Go programs usually want to wait for a single or a smaller number of processes, not for all processes. PIDs of processes are usually obtained by other means than searching the list of all processes.
If you are on Linux, the list of processes can be obtained by reading contents of <code>/proc</code> directory. See question https://stackoverflow.com/questions/939778/linux-api-to-list-running-processes
答案2
得分: 21
这个库:
github.com/mitchellh/go-ps
对我有用。
import (
ps "github.com/mitchellh/go-ps"
... // 其他导入在这里...
)
func whatever(){
processList, err := ps.Processes()
if err != nil {
log.Println("ps.Processes() 失败,你是否在使用Windows?")
return
}
// 映射年龄
for x := range processList {
var process ps.Process
process = processList[x]
log.Printf("%d\t%s\n",process.Pid(),process.Executable())
// 在pid上执行os.*操作
}
}
英文:
This library:
github.com/mitchellh/go-ps
worked for me.
import (
ps "github.com/mitchellh/go-ps"
... // other imports here...
)
func whatever(){
processList, err := ps.Processes()
if err != nil {
log.Println("ps.Processes() Failed, are you using windows?")
return
}
// map ages
for x := range processList {
var process ps.Process
process = processList[x]
log.Printf("%d\t%s\n",process.Pid(),process.Executable())
// do os.* stuff on the pid
}
}
答案3
得分: 10
我建议为此目的使用以下库:
https://github.com/shirou/gopsutil/
以下是获取总进程数和正在运行的进程数的示例代码:
package main
import (
"fmt"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
)
func main() {
infoStat, _ := host.Info()
fmt.Printf("总进程数:%d\n", infoStat.Procs)
miscStat, _ := load.Misc()
fmt.Printf("正在运行的进程数:%d\n", miscStat.ProcsRunning)
}
该库允许获取其他多种数据。
请查看文档以获取根据目标操作系统提供的可用信息。
英文:
I suggest to use for this purpose the following library:
https://github.com/shirou/gopsutil/
Here is an example to get total processes and running ones:
package main
import (
"fmt"
"github.com/shirou/gopsutil/host"
"github.com/shirou/gopsutil/load"
)
func main() {
infoStat, _ := host.Info()
fmt.Printf("Total processes: %d\n", infoStat.Procs)
miscStat, _ := load.Misc()
fmt.Printf("Running processes: %d\n", miscStat.ProcsRunning)
}
The library allows to get several other data.
Take a look at the documentation for available informations provided according to the target operative system.
答案4
得分: 5
如果您只需要进程信息,可以从您的Go代码中运行“ps”命令,然后解析文本输出。
完整的解决方案可以参考《Learning Go》书中的第29个练习 @ http://www.miek.nl/files/go/
英文:
If you only need the process information, can just run "ps" command from your go code, then parse the text output.
A complete solution can refer to Exercise 29 in Book "Learning Go" @ http://www.miek.nl/files/go/
答案5
得分: 4
在这个库中,你还可以获取其他进程信息。
英文:
you can use this library github.com/shirou/gopsutil
package main
import (
"fmt"
"github.com/shirou/gopsutil/v3/process"
)
func main() {
processes, _ := process.Processes()
for _, process := range processes {
name, _ := process.Name()
fmt.Println(name)
}
}
in this library,you can also get process info other
答案6
得分: 2
我找到了一个相当简单的解决方案,可以在不使用大型库的情况下获取正在运行的进程列表:
package main
import (
"fmt"
"os"
"path/filepath"
)
func main() {
matches, err := filepath.Glob("/proc/*/exe")
for _, file := range matches {
target, _ := os.Readlink(file)
if len(target) > 0 {
fmt.Printf("%+v\n", target)
}
}
}
它将打印每个正在运行的进程的路径。如果你只需要进程名称,可以使用filepath.Base(target)
来获取。
这个方法通过解引用/proc/[procID]/exe
文件的符号链接来工作,该符号链接指向可执行文件。这比从/proc/[procID]/status
文件中读取和提取进程名称要简单得多(如其他解决方案中所建议的)。
PS:这可能在所有发行版上都不起作用,因为它依赖于进程文件夹中的exe
文件,而这在所有Linux版本中可能都不存在。
英文:
For linux
I found a fairly simple solution to get the list of running processes without using a large library:
package main
import (
"fmt"
"os"
"path/filepath"
)
func main() {
matches, err := filepath.Glob("/proc/*/exe")
for _, file := range matches {
target, _ := os.Readlink(file)
if len(target) > 0 {
fmt.Printf("%+v\n", target)
}
}
}
It will print the path for each running process. If you need just the process name, then you can get it with filepath.Base(target)
This works by de-referencing the symlink for the /proc/[procID]/exe
file, which is a symlink to the executable file. This is much simpler than reading and extracting the process name from the /proc/[procID]/status
file (as suggested in other solutions I found).
PS: This might not work on all distribution because it relies on the exe
file in the process' folder, which might not present in all flavors of Linux.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论