如何在读取命名管道

时间:2018-03-28 23:46:54

标签: linux bash pipe named-pipes tail

我有一个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

输出
# 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 +++

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

1 个答案:

答案 0 :(得分:1)

尝试使用

拖尾文件

tail --follow=name --retry

您可能需要按照here所述调整nfs mount。