zoukankan      html  css  js  c++  java
  • tail -f 不好用? 用法小解析

    首先要感谢100868502群中的清风童鞋o(^▽^)o

    【转】http://geeklu.com/2010/06/continuous-monitor-tail-fails/

    问题描述

    在测试tail -f的时候,发现一个奇怪的现象,首先 我在一个窗口中 tail -f test.txt 然后在另一个窗口中用vim编辑这个文件,增加了几行字符,并保存,这个时候发现第一个窗口中并没有变化,没有将最新的内容显示出来。

    查找原因

    然后我查看了以下tail的帮助,看到了下面的内容

    -F same as –follow=name –retry

    我就加上-F,重复上面的实验过程, 发现这次有变化了,但是是完全的重新加载了一次,

    tail: `test.txt’ has been replaced; following end of new file

    这个提示表明,tail原来跟踪的文件被替换了,所以重新加载了一次。

    这个时候把目光转移到vim。 这个时候重新实验,tail -f test.txt,然后vim编辑,保存。 通过lsof|grep test.txt(列出打开test.txt文件的进程) 发现tail -f跟踪的文件在vim编辑后是被删除掉了。

    保存前

    luke@luke-desktop:~/Desktop$ lsof|grep test.txt tail      10158       luke    3r      REG                8,2        0    4984552 /home/luke/Desktop/test.txt vim       10173       luke    4u      REG                8,2    12288    4981647 /home/luke/Desktop/.test.txt.swp

    vim保存后

    luke@luke-desktop:~/Desktop$ lsof|grep test.txt tail      10158       luke    3r      REG                8,2        0    4984552 /home/luke/Desktop/test.txt~ (deleted) vim       10173       luke    4u      REG                8,2    12288    4981647 /home/luke/Desktop/.test.txt.swp

    这个时候再去细读了下tail的帮助文件。

    With –follow (-f), tail defaults to following the file descriptor, which means that even if a tail’ed file is renamed, tail will continue to track its end. This default behavior is not desirable when you really want to track the actual name of the file, not the file descriptor (e.g., log rotation). Use –follow=name in that case. That causes tail to track the named file by reopening it periodically to see if it has been removed and recreated by some other program.

    看了之后明白了一切。

    结论

    当使用vim编辑保存一个文件之后,vim创建了一个新的文件,老的文件被删除,所以他们的inode不一样了。 而tail -f 默认情况下是根据文件描述符来进行文件跟踪的,而文件描述符创建的时候又是依赖于inode的,所以vim保存之后不会被tail -f发现(因为是新的inode)。

    PS:我们可以通过下面的方式来进行测试 echo testest»test.txt

    【转】 https://www.douban.com/note/85851188/

    tail -f 和 -F 的用法

     
  • 相关阅读:
    settings.xml的配置
    查看Linux防火墙状态
    Linux系统安装jdk并配置环境变量
    Windows系统与虚拟机CentOS之间文件复制
    搭建公司后台服务架构(1)
    09-http.ts配置了全局的http拦截器,单独某个组件不想要这个拦截器,如何设置
    17- vue自定义指令-操作DOM的
    15-keep-alive
    14-观察者模式和发布订阅的区别/vue响应式是发布订阅模式和观察者模式
    13.每个vue文件都是一个私有作用域/css的私有性原理
  • 原文地址:https://www.cnblogs.com/my-ShowTime/p/5404696.html
Copyright © 2011-2022 走看看