"ls"在使用查找命令时由信号 13 终止



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

find: ‘ls’ terminated by signal 13

我的问题很简单,我该如何阻止这些?我想我明白这些是由于我的 find 命令和 head -n 10 命令同时运行,因为 head 在查找后被管道传输,但如果有人可以详细说明,那就太好了。我的目标是将其提交给正在工作的权力(沃尔玛(,以便开始在我们的测试服务器上运行它。

另请注意,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

没什么好担心的。 这是一个断开的管道,因为头部将在所有行写入 stdout 之前完成前 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

这将花费您一些时间,但它可以在不改变结果的情况下起作用。

该消息无害,但很烦人。发生这种情况是因为head在填满输入行时退出,但find继续运行并执行更多ls调用。这些ls命令试图打印并最终被SIGPIPE杀死,因为没有人再听它们了。

您可以使用2>/dev/null来隐藏错误,但这也会隐藏其他合法错误。一种更外科手术的方法是:

find ... ; 2> >(grep -v 'terminated by signal 13' >&2) | head -n 10

这使用进程替换来仅筛选出一条消息。2>将 stderr 重定向到grep>&2将任何幸存的消息重定向回 stderr。

这并不完美,因为find不知道它应该退出。在退出很久之后,它仍然注定要ls运行head

最新更新