因此,我尝试运行一个脚本,该脚本在df -h的内容中搜索超过阈值的目录,并在该目录上运行一个find命令,以获取前十个最大的文件,然后停止运行find。但是,当它按照预期给出前十个文件时,它会多次输出:
find: ‘ls’ terminated by signal 13
我的问题很简单,我如何阻止这些?我想我明白这是因为我的find命令和head -n 10命令同时运行,因为head是在find之后通过管道传输的,但如果有人能详细说明,那就太好了。我的目标是将它提交给正在工作的力量(沃尔玛),让它开始在我们的测试服务器上运行。
下面还要注意,find命令中的大小只有5M,因为我的测试服务器上没有该目录中那么大的10个文件。
下面是脚本:
#!/bin/bash
#defines what exceeds threshold
threshold="90%|91%|92%|93%|94%|95%|96%|97%|98%|99%|100%|6%"
#puts the output of df -h into output_df
df -h > output_df
cat output_df | awk -v VAR=$threshold '{if($5~VAR)print $6}' > exceeds_thresh
LINES=()
while IFS= read -r exceeds_thresh
do
find $exceeds_thresh -xdev -size +5M -exec ls -lah {} \; | head -n 10
done < "exceeds_thresh"
#cleaning up the files the script created
rm output_df exceeds_thresh
下面是一个示例输出:
-rw-r-----+ 1 root systemd-journal 16M Jun 1 19:18 /var/log/journal/a237b5bc574941af85c796e15b0ce420/system.journal
-rw-r-----+ 1 root systemd-journal 8.0M May 29 05:38 /var/log/journal/a237b5bc574941af85c796e15b0ce420/system@00056d51a41389f0-0c1bef27b9d68ad6.journal~
-rw-r-----+ 1 root systemd-journal 104M Jun 1 05:55 /var/log/journal/a237b5bc574941af85c796e15b0ce420/system@45697f9ed4b84f07b92c5fcbc8a945bd-0000000000000001-00056d51a40f2f0c.journal
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
find: ‘ls’ terminated by signal 13
发布于 2018-06-02 03:30:41
这没什么好担心的。这是一个损坏的管道,因为在所有行都写入stdout之前,head将完成前10行的读取。
你可以在结尾处使用>/dev/null 2>&1
来使其静默,或者使用2>/dev/null
来静默错误。我还看到了另一个将tail -n +1
添加到管道中的技巧:
find $exceeds_thresh -xdev -size +5M -exec ls -lah {} \; | tail -n +1 | head -n 10
这将花费你一些时间,但它将在不改变结果的情况下工作。
发布于 2018-06-02 03:44:23
这条消息是无害的,但很烦人。之所以会发生这种情况,是因为head
在输入行填满后退出,而find
仍在运行和执行更多的ls
调用。这些ls
命令试图打印,但最终被一个SIGPIPE终止,因为再也没有人在听它们了。
您可以使用2>/dev/null
来隐藏错误,但这也会隐藏其他合法的错误。一种更具外科性质的方法是:
find ... \; 2> >(grep -v 'terminated by signal 13' >&2) | head -n 10
这将使用process substitution过滤掉只有一条消息。2>
将stderr重定向到grep
中,>&2
将所有存活的消息重定向回stderr。
这并不完美,因为find
不知道它应该退出。在head
退出之后很长一段时间,它仍然运行着注定失败的ls
es。
https://stackoverflow.com/questions/50649866
复制相似问题