linux忽略写入的报错信息,linux – sort:写入失败|破管

晚上好,

以下是我在脚本中使用的一段代码.从SSH sesssion启动工作正常,但是,当它通过cron运行时,它会在屏幕上显示损坏的管道错误.

我不能通过SSH重现它.

码:

IP=$(sort --random-sort /root/ips.csv | head -n 1); nc -zv -w 2 $IP 443 2>&1 | grep succeeded >> outfile

屏幕错误:

sort: write failed: standard output; Broken pipe

sort: write error

任何提示/指针?

谢谢!

解决方法:

当头部在处理完第一条线后完成时,它会退出,关闭管道的另一端. sort可能仍在尝试编写更多内容,写入已关闭的管道或套接字会返回EPIPE错误.但它也会引发SIGPIPE信号,从而导致进程无效,除非忽略或处理信号.忽略信号后,sort会看到错误,抱怨和退出.但是如果信号没有被忽略,它就会死掉,而不会留下错误信息.

忽略信号会继承到子进程,因此我们可以从shell控制它:

$ trap - PIPE # set default action for signal

$sort bigfile | head -1 > /dev/null # no error message

$trap "" PIPE # ignore the signal

$sort bigfile | head -1 > /dev/null

sort: write failed: standard output: Broken pipe

sort: write error

That’s

not really useful for normal daemons though, and as we try to provide a

good, useful execution environment for daemons, we turn this off. Of

course, shells and suchlike should turn this on again.

在我的系统上,/ lib / system / system / cron.service显式撤消了cron的默认值,设置了IgnoreSIGPIPE = false.

标签:head,bash,linux,grep,sort

来源: https://codeday.me/bug/20190813/1646748.html