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. ios开发图层layer与核心动画二:CATransform3D,CAlayear和UIView区别,layer的position和anchorpoint

    一:CATransform3D #import "ViewController.h" @interface ViewController () @property (weak, n ...

  2. 【Lucene4.8教程之二】索引 2014-06-16 11:30 3845人阅读 评论(0) 收藏

    一.基础内容 0.官方文档说明 (1)org.apache.lucene.index provides two primary classes: IndexWriter, which creates ...

  3. [Linux] Use find to search for filename patterns

    Learn how to use find to identify filenames matching specified patterns. We'll use find to identify ...

  4. php中的var关键字的用法总结(var在类外用报错)(类属性必须带限定词)

    php中的var关键字的用法总结(var在类外用报错)(类属性必须带限定词) 一.总结 1.var在类外用报错:如果不是在类中,用var定义变量是错的. 2.类属性必须带限定词:php中类属性必须定义 ...

  5. D3.js以及通用JS(JavaScript)读取并解析server端JSON的注意事项

    这个需求事实上挺明白的.可是网上搜出来的教程都乱七八糟,认为实在须要自己总结一下. D3.js眼下已经被广泛使用在数据可视化领域,随着大数据的持续发展,这个框架预计会在今后更加流行(据说其作者Mike ...

  6. 实现上拉加载更多的SwipeRefreshLayout

    转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/49992269 本文出自:[江清清的博客] (一).前言: [好消息] ...

  7. .netcore consul实现服务注册与发现-单节点部署

    原文:.netcore consul实现服务注册与发现-单节点部署 一.Consul的基础介绍     Consul是HashiCorp公司推出的开源工具,用于实现分布式系统的服务发现与配置.与其他分 ...

  8. 【u109】数字生成游戏(gen)

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 小明完成了这样一个数字生成游戏,对于一个不包含0的数字s来说,有以下3种生成新的数的规则: 1. 将s ...

  9. error: invalid abbreviation code [25] for DIE at 0x0000003e in Assertion failed: (*offset_ptr == end_prologue_offset), function ParsePrologue, file /S

    error: invalid abbreviation code [25] for DIE at 0x0000003e in '/Users/mac/Desktop/MYiosfiles/test/X ...

  10. mingw-w64线程模型:posix vs win32(posix允许使用c++11的std:: thread,但要带一个winpthreads,可能需要额外dll)

    我正在安装 mingw-w64 on Windows,有两个选项: win32线程和posix线程. 我知道win32线程和pthreads之间的区别,但是我不明白这两个选项之间的区别. 我怀疑如果我 ...