使用 find 命令时,‘ls’ 被信号 13 终止

‘ls’ terminated by signal 13 when using find command

所以我正在尝试 运行 一个脚本,该脚本在 df -h 的内容中搜索超过阈值的目录,基本上 运行 该目录上的查找命令以获取顶部十个最大的文件,然后停止 运行ning 查找。但是,当它按预期给出前十个文件时,它会多次吐出:

find: ‘ls’ terminated by signal 13

我的问题很简单,我该如何阻止这些?我想我明白这些是由于我的 find 命令和 head -n 10 命令 运行ning 同时出现,因为 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(~VAR)print }' > 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

没什么好担心的。这是一个损坏的管道,因为 head 将在所有行写入 stdout 之前完成读取前 10 行。

您可以在末尾使用 >/dev/null 2>&12>/dev/null 来消除错误。我还看到了将 tail -n +1 添加到管道中的另一个技巧:

find $exceeds_thresh -xdev -size +5M -exec ls -lah {} \; | tail -n +1 | head -n 10

这会花费您一些时间,但不会改变结果。

该消息无害,但令人讨厌。发生这种情况是因为 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