【发布时间】: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