exec.Command在Go的pprof工具中没有注册错误。

huangapple go评论78阅读模式
英文:

exec.Command does not register error from Go's own pprof tool

问题

这是我的代码:

cmd := exec.Command("go", "tool", "pprof", "-dot", "-lines", "http://google.com")
out, err := cmd.Output()
if err != nil {
    panic(err)
}
println(string(out))

当我在控制台中运行完全相同的命令时,我看到:

$ go tool pprof -dot -lines http://google.com 
正在从 http://google.com/profilez 获取配置文件
请稍候...(30秒)
服务器响应:404 Not Found

然而,我的Go程序没有注册这是一个错误。奇怪的是,变量out打印为空字符串,err为nil。发生了什么?

为了澄清,我正在对http://google.com进行性能分析,以故意创建一个错误。通常我会对一个真实的Go应用程序进行性能分析。

英文:

Here is my code:

cmd := exec.Command("go", "tool", "pprof", "-dot", "-lines", "http://google.com")
out, err := cmd.Output()
if err != nil {
    panic(err)
}
println(string(out))

When I run the exact same command in my console, I see:

$ go tool pprof -dot -lines http://google.com 
Fetching profile from http://google.com/profilez
Please wait... (30s)
server response: 404 Not Found 

However, my go program does not register that this is an error. Oddly, the variable out prints as an empty string and err is nil. What is going on?

To clarify, I am profiling http://google.com to purposefully create an error. I would normally profile a real Go application.

答案1

得分: 2

获取个人资料从http://google.com/profilez
请稍等...(30秒)
服务器响应:404未找到

写入到stderr。您的程序捕获stdout,但stdout为空。考虑调用:

out,err:= cmd.CombinedOutput()

来获取stdout和stderr。

cmd.Output()和cmd.CombinedOutput()返回err == nil,因为命令以零状态退出。也许应该提出一个问题,要求命令以非零状态退出。

英文:

The text

Fetching profile from http://google.com/profilez
Please wait... (30s)
server response: 404 Not Found 

is written to stderr. Your program captures stdout, which is empty. Consider calling:

out, err := cmd.CombinedOutput()

to grab both stdout and stderr.

cmd.Output() and cmd.CombinedOutput() return err == nil because the command exits with status zero. Perhaps an issue should be filed requesting that the command exit with non-zero status.

huangapple
  • 本文由 发表于 2015年7月2日 11:03:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/31174943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定