我正在尝试获取进程列表,包括它们的当前负载。
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执行此命令将以相同的权限运行。
答案 0 :(得分:0)
看起来您需要删除转义的引号。外壳程序(或本例中的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.