【问题标题】:How can I prevent read block from exiting while reading named pipe读取命名管道时如何防止读取块退出
【发布时间】:2018-03-28 23:46:54
【问题描述】:

我有一个 BASH 脚本,它在 NFS 挂载的文件系统上跟踪文件并写入名为 /tmp/t2 的管道。

#!/bin/bash
tail -f /mnt/cdr1/radius/calldatarecords | grep ^2 | awk -F, '{print $4","$13","$14","$15","$22","$23","$120","$140","$173}' | tr -d \" | tr - _  > /tmp/t2

挂载输出

# mount
v7000:/ibm/cdr-fs on /mnt/cdr1 type nfs (ro,relatime,vers=3,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,mountaddr=10.12.2.12,mountvers=3,mountport=32767,mountproto=udp,local_lock=none,addr=10.12.2.12,_netdev)

第二个脚本从阻塞的 /tmp/t2 管道中读取

#!/bin/bash
echo "connected to pipe"
while read CID DUR CST DCD EGR ING PDD SGL MST
do
      echo Read Line at $(date +%s)
done < /tmp/t2
echo "what should never happen"

这已经在 Ub12.04 上运行多年,只有很少的故障,然后在迁移到 Ub16.04 LTS 后开始频繁发生。最初编码时,当 /mnt/cdr1/radius/calldatarecords 在晚上没有被一致地写入时,我看到了这种性质的东西。失败的读取块周围的一段时间确实在当时大部分解决了该问题,但是不再有效。不应该发生的事情在 16.04 经常发生。我已经在 strace 下运行了这两个脚本来捕捉正在发生的事情。第二个程序退出

write(1, "Read Line at 1522279814\n", 24Read Line at 1522279814
) = 24
ioctl(0, TCGETS, 0x7ffc48240330)        = -1 ENOTTY (Inappropriate ioctl for device)
lseek(0, 0, SEEK_CUR)                   = -1 ESPIPE (Illegal seek)
read(0, "", 1)                          = 0
dup2(10, 0)                             = 0
fcntl(10, F_GETFD)                      = 0x1 (flags FD_CLOEXEC)
close(10)                               = 0
read(255, "echo \"this should never happen\"\n", 158) = 32
write(1, "this should never happen\n", 25this should never happen
) = 25
read(255, "", 158)                      = 0
rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
exit_group(0)                           = ?
+++ exited with 0 +++

因此第一个脚本 strace 退出报告 SIGPIPE。或者也许反过来,考虑到我不将此数据识别为 calldatarecords 的一部分。

...
read(5, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 8192) = 4424
write(1, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"..., 4096) = -1 EPIPE (Broken pipe)
--- SIGPIPE {si_signo=SIGPIPE, si_code=SI_USER, si_pid=12853, si_uid=0} ---
+++ killed by SIGPIPE +++

有人可以解释为什么第二个程序异常退出以及如何防止它吗?任何帮助表示赞赏。感谢阅读。

【问题讨论】:

    标签: linux bash pipe named-pipes tail


    【解决方案1】:

    尝试使用

    来跟踪您的文件

    tail --follow=name --retry

    您可能需要按照here 的说明调整 nfs 挂载。

    【讨论】:

    • 我已经尝试过尾部开关 --follow=name、--retry 甚至是未记录的 ---disable-inotify。我接受了您的建议,并使用 NFS noac 重新安装,如果有任何问题,它现在会更快地失败。
    • 尝试拖尾到常规文件以排除命名管道效应。如果失败,则问题与 NFS 有关。
    • tail 在 NFS 和本地文件系统上的文件上都可以正常工作,但是存在根本区别,因为它们没有被主动写入。我试图模拟它,但它没有失败。非法寻求会是问题的一部分吗?为什么 bash 试图在读取等线性操作中寻找不是文件的文件。
    猜你喜欢
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多