英文:
ignore output of 'broken pipe' signal
问题
在执行命令(在Linux中)时:
go run test.go |head
输出结果为:
line 0
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
signal: broken pipe
如何防止程序打印信号通知(最后一行)?
[顺便说一下:即使使用2>/dev/null
也不起作用。可能是最后一行输出到TTY]
英文:
When executing a command (in Linux):
go run test.go |head
The output is:
line 0
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
signal: broken pipe
How to prevent the program from printing the signal notification (last line) ?
[BTW: even 2>/dev/null
dosn't work. probably the last line outputed to TTY]
答案1
得分: 1
你没有打印代码test.go
,所以很难判断。
例如,它在打印之间是否有休眠?
破管错误可能是由于管道链中的一个程序提前终止引起的:
a.exe|b.exe|c.exe|d.exe
在test.go
终止时,head仍在等待输入,或者更有可能是head在test.go
完成运行之前终止。特别是因为head的默认行数是10行。
对此进行测试的方法是执行go run test.go |head -20
,看看在打印20行后是否出现破管错误。
我无法在C++中复现这个问题,也不了解go语言。如果这是go运行时的结果,我建议你在代码中捕获SIGPIPE信号并安静地终止程序(实际上无论使用哪种语言,过程都是相同的,只是语法不同)。
否则,这更多是bash/zsh或其他当前使用的shell的问题。在运行命令之前,为SIGPIPE信号创建一个信号处理程序,以便能够优雅地退出。
英文:
You don't print the code test.go
so it is hard to judge.
For example does it sleep between prints?
Broken pipe would happpen because of a one of the programs in a chain of pipes:
a.exe|b.exe|c.exe|d.exe
terminates early. Either test.go
terminates while head is still expecting
input or more likely head terminates before test.go
finishes running. Especially since the default for head is 10 lines.
The test for that would be to do go run test.go |head -20
and see if you get a broken pipe after printing 20.
I was not able to replicate this in C++, and don't know go. If it is the result of the go runtime, I suggest you try catching SIGPIPE in your code and terminating quietly. ( Doesn't matter what language really you, the process is the same the syntax different. )
Otherwise this is more a bash/zsh/whatever the shell du jour is. Before you run the command make a signal handler for SIGPIPE to exit cleanly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论