简体   繁体   English

尾巴-f不显示最后几行

[英]Tail -f doesn't show last lines

I'm doing a scrip that reads from a log and send the line throught netcat 我正在做一个从日志中读取并通过netcat发送线路的脚本

tail -f /tmp/archivo.txt | grep --line-buffered status=bounced | while read LINE0 
do 
    echo "${LINE0}"
    echo "${LINE0}" > /tmp/mail-line.log
    netcat localhost 5699 < /tmp/mail-line.log 
    sleep 1s
done

When I first launch this script it properly sends the data but when a new line is introduced it doesn't work unless I relaunch the script, any ideas? 当我第一次启动该脚本时,它会正确发送数据,但是当引入新行时,除非重新启动该脚本,否则它将无法工作,有什么想法吗? Thank you. 谢谢。

Edit: 编辑:

As @Kamil Cuk asked me, I tried only doing echos, it didn't work 正如@Kamil Cuk询问的那样,我只尝试做回声,但没有用

What's happening?: 发生了什么?:

Well, I was introducing the new data using gedit, but this didn't work whit the -f flag but instead with the -F flag because it says that archivo.txt has been replaced. 好吧,我正在使用gedit引入新数据,但这在-f标志下无法正常工作,而在-F标志下却无法工作,因为它说archivo.txt已被替换。 I tried doing echo "New line with bounce=status" >> archivo.txt" and it worked. So I'm assuming that gedit somehow changes metadata and tail doesn't show anything with -f thats why it's not working. 我想这样做echo "New line with bounce=status" >> archivo.txt" ,它的工作,所以我假设的gedit在某种程度上改变了元数据和尾部没有显示任何东西-f这就是为什么它不工作。

Under the hood files in the filesystem are just identified by numbers, called " inodes ". 在后台,文件系统中的文件仅由数字标识,称为“ inodes ”。 Then you have a text label that points to the inode, called a "file name". 然后,您有一个指向索引节点的文本标签,称为“文件名”。 When you run tail -f filename you start tailing the file behind the inode that filename currently points to; 当运行tail -f filename ,开始将文件拖尾到文件filename当前指向的inode后面; this is called "opening a file handle " because the tail program creates an open reference to the file through a "file handle". 这被称为“打开文件句柄 ”,因为tail程序通过“文件句柄”创建了对文件的开放引用。

What happened in your case is that you started tailing one file. 您的情况是您开始拖尾一个文件。 Then your logging application (gedit :-) ) created a new file on a different inode and changed what inode filename refers to. 然后,您的日志记录应用程序(gedit :-))在另一个inode上创建了一个文件,并更改了inode filename所指的内容。 The file at the inode that you are tailing still exists because tail still have an open file handle, it won't get removed until all programs close their file handles. 您要拖尾的inode上的文件仍然存在,因为tail仍然具有打开的文件句柄,只有在所有程序关闭它们的文件句柄后,它才会被删除。 But any new program that tries to open the file will get to the new inode. 但是,任何尝试打开文件的新程序都将进入新的inode。

To get around this you need to open the file in "append mode" (there are other modes as well). 要解决此问题,您需要在“附加模式”(还有其他模式)下打开文件。 "Append mode" means you are pushing data to the end of the file without creating a new inode. “追加模式”表示您将数据推送到文件的末尾而不创建新的inode。 All real logging applications will do this since it is much faster than rewriting the entire file every time. 所有实际的日志记录应用程序都将执行此操作,因为它比每次重写整个文件都要快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM