tail [OPTION]... [FILE]...
-c, --bytes=K            output the last K bytes; alternatively, use -c +K
                           to output bytes starting with the Kth of each file
  -f, --follow[={name|descriptor}]
                           output appended data as the file grows;
                           -f, --follow, and --follow=descriptor are
                           equivalent
  -F                       same as --follow=name --retry
  -n, --lines=K            output the last K lines, instead of the last 10;
                           or use -n +K to output lines starting with the Kth
      --max-unchanged-stats=N
                           with --follow=name, reopen a FILE which has not
                           changed size after N (default 5) iterations
                           to see if it has been unlinked or renamed
                           (this is the usual case of rotated log files).
                           With inotify, this option is rarely useful.
      --pid=PID            with -f, terminate after process ID, PID dies
  -q, --quiet, --silent    never output headers giving file names
      --retry              keep trying to open a file even when it is or
                             becomes inaccessible; useful when following by
                             name, i.e., with --follow=name
  -s, --sleep-interval=N   with -f, sleep for approximately N seconds
                             (default 1.0) between iterations.
                             With inotify and --pid=P, check process P at
                             least once every N seconds.
  -v, --verbose            always output headers giving file names
      --help     display this help and exit
      --version  output version information and exit
其中, 使用tail -F -n +K logfile, 可以很好地收集日志数据. 是以前理解错误! 纠正, 纠正!
在日志切换时会打印错误信息:
im e

tail: `/home/hadoop/test.log' has become inaccessible: No such file or directory
tail: `/home/hadoop/test.log' has appeared;  following end of new file
this is a new file
其中第1句可以使用--retry去掉, 第2句则暂不确定.最好就是阅读非"tail:"打头的行才记入日志.
测试代码:
public static void main(String[] args) throws IOException, InterruptedException {
  ProcessBuilder pb = new ProcessBuilder("bash");
  pb.redirectErrorStream(true);
  final Process p = pb.start();
  PrintStream stdin = new PrintStream(p.getOutputStream());
  // stdin.println("source /etc/profile");
  stdin.println("tail -F -q --retry  ~/test.log");

stdin.close();

Thread t = new Thread(new Runnable() {
   public void run() {
    StringWriter buf = new StringWriter();
    PrintWriter pbuf = new PrintWriter(buf);
    // Scanner stdout = new Scanner(p.getInputStream());
    BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line = null;
    while (true) {
     try {
      line = stdout.readLine();
      if (line == null) {
       return;
      }
      System.out.println(line);
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }

}

}
  });
  t.setDaemon(true);
  t.start();
  p.waitFor();
  System.out.println(p.exitValue());
  System.out.println("done...");
}

Linux之tail命令实时收集[纠正误解]的更多相关文章

  1. Linux的watch命令 — 实时监测命令的运行结果

    Linux的watch命令 — 实时监测命令的运行结果 watch 是一个非常实用的命令,基本所有的 Linux 发行版都带有这个小工具,如同名字一样,watch 可以帮你监测一个命令的运行结果,省得 ...

  2. Linux下tail命令的使用方法

    Linux下tail命令的使用方法: linux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新,ta ...

  3. 【linux】tail 命令详解

    转自:https://www.cnblogs.com/fps2tao/p/7698224.html Linux命令:显示文件结尾 Head/Tail head 与 tail 就像它的名字一样的浅显易懂 ...

  4. Linux head/tail命令详解

    head命令用于显示文件的开头的内容.在默认情况下,head命令显示文件的头10行内容. tail命令用于显示文件的结尾的内容.在默认情况下,taild命令显示文件的后10行内容. head常见命令参 ...

  5. Linux下tail命令

    简述 tail命令从指定点开始将文件写到标准输出,使用tail命令的“-f”选项可以方便的查阅正在改变的日志文件,“tail -f filename”会把filename里最尾部的内容显示在屏幕上,并 ...

  6. Linux 下 tail 命令

    简述 tail命令从指定点开始将文件写到标准输出,使用tail命令的“-f”选项可以方便的查阅正在改变的日志文件,“tail -f filename”会把filename里最尾部的内容显示在屏幕上,并 ...

  7. 【Linux】tail命令

    用途 tail命令主要用于取出后边几行 全称 tail命令的全称即为tail(尾巴) 参数 -n :后边接数字,代表显示几行的意思 -f :循环读取 -q :不显示处理信息 -v :显示详细的处理信息 ...

  8. Linux的tail命令查看文件

    小文件一般用cat  查看,但是如果文件内容过多,用cat就不合适了 可以用tail命令 # 默认显示文件最后十行 tail a.txt # 监视文件的尾部内容,默认十行, 可以-n 20显示20行 ...

  9. 使用tail命令实时查看日志文件

    [Shell] 纯文本查看 复制代码 ? 1 tail -f /日志文件 好了.就这样用.简单吧    退出ctrl+C

随机推荐

  1. SpringBoot 使用 @Value 从 YAML文件读取属性(转)

    在 YAML中有如下配置 paypal: mode:live 在类中,通过 @Value属性读取 @Value("${paypal.mode}") private String m ...

  2. TF-IDF计算方法和基于图迭代的TextRank

    文本处理方法概述 说明:本篇以实践为主,理论部分会尽量给出参考链接 摘要: 1.分词 2.关键词提取 3.主题模型(LDA/TWE) 4.词的两种表现形式(词袋模型和分布式词向量) 5.关于文本的特征 ...

  3. Docker CE for Windows安装使用

    原文:Docker CE for Windows安装使用 官网下载并安装Docker CE for Windows IDEA连接Docker Docker一些常用命令 Docker for windo ...

  4. USB 3.0规范中译本 第4章 超高速数据流模型

    本文为CoryXie原创译文,转载及有任何问题请联系cory.xie#gmail.com. 本章展示数据和信息如何在超高速上通过的一种高层次的描述.请阅读协议层一章关于低层次协议的细节.本章提供设备架 ...

  5. Android中的动画详解系列【3】——自定义动画研究

    在上一篇中我们使用到了位移动画TranslateAnimation,下面我们先来看看TranslateAnimation是如何实现Animation中的抽象方法的: /* * Copyright (C ...

  6. php中如何获取数组长度

    php获取数组的长度的方法 一.总结 一句话总结:count方法和sizeof方法 二.php获取数组的长度的方法 php获取数组长度的方法: 一). 获取一维数组的方法: 1.count.sizeo ...

  7. [Angular] Create dynamic content with <tempalte>

    To create a dynamic template, we need to a entry component as a placeholder. Then we can use entry c ...

  8. [_UICascadingTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds

    之前写过一篇<如何更好地限制一个UITextField的输入长度>,在文章最后得到的结论是可以直接使用 UIKIT_EXTERN NSString *const UITextFieldTe ...

  9. php数学和时间常用函数有哪些(总结表)(看学习视频效率挺高的)(复习)

    php数学和时间常用函数有哪些(总结表)(看学习视频效率挺高的)(复习) 一.总结 一句话总结: 1.数学函数常用的6个:max().min().cell().floor().round().mt_r ...

  10. 电子商务网站的设计与实现(四):项目名称malling和一期开发计划

    项目名称:小雷B2C商城系统代号:malling,mall商城的意思,加个ing表示正在进行.更主要的是与"morning"发音很像,非常容易记忆.morning是"早上 ...