windows - 从 Go 代码调用 typeperf 以接收系统信息

标签 windows go

我正在尝试获取进程列表,包括它们的当前负载。 typeperf "\Process(*)\% Processor Time"-sc 1 似乎给了我想要的输出。从 cmd 执行此操作。

现在我尝试从我的 Go 代码中调用该命令

// Command to list processes
cmdPS := exec.Command("typeperf", "\"\\Process(*)\\% Processor Time\"", "-sc", "1")
cmdPS.Stdout = &buff
cmdPS.Stderr = &errBuff
err := cmdPS.Run()
if err != nil {
    log.Printf("Err: %s", buff.String())
    return nil, errors.Wrapf(err, "Failed to call: ps ax -o")
}

这导致:

Exiting, please wait...
Error: No valid counters.

Note:
  In order to use typeperf, you must either be a member of the local
  Performance Log Users group, or the command must be executed from an
  elevated command window.

我格式化命令字符串的方式有误吗?我希望直接执行此命令并通过 Go 执行它会以相同的权限运行。

最佳答案

看起来您需要删除转义引号。 shell(或本例中的 cmd)将为您处理参数。

但是,在我的机器上运行 typeperf "\Process(*)\% Processor Time"-sc 1 会产生以下结果。

Error: No valid counters.

Note:
  In order to use typeperf, you must either be a member of the local
  Performance Log Users group, or the command must be executed from an
  elevated command window.

关于windows - 从 Go 代码调用 typeperf 以接收系统信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52873127/

相关文章:

go - 由于 "Authentication failed: EOF",Mesos 框架保持不活动状态

go - 脚踏车停运时的脚踏车工作流程

go - 围棋练习之旅#10 : Crawler

http - 创建要通过 Google App Engine 提供的 tar 文件

windows - Windows 密码中的符号

c# - 在小型 .NET 程序中管理临时文件

c# - 有什么方法可以从 Windows 7 上的 Windows 服务启动 GUI 应用程序?

c# - 在 .Net 3.5 中使用 Ninject 时 System.Core 的 System.Io.FileNotFoundException

go - -bash : set: -g: invalid option when I setting GOPATH

c - 使用 C 检索内存插槽数