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

我正在尝试获取进程列表,包括它们的当前负载。 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 执行它会以相同的权限运行。


郎朗坤
浏览 189回答 1
1回答

慕婉清6462132

看起来您需要删除转义引号。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.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go