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...");
}

最新文章

  1. 图解Javascript原型链
  2. Hadoop日志文件分析系统
  3. iOS开发Swift篇—(三)字符串和数据类型
  4. 如何分割一个utf8字符串(保证单个汉字的完整性)
  5. IIS8中部署WCF服务出错:HTTP 错误 404.3 - Not Found
  6. linux笔记:RPM软件包管理-源码包管理
  7. iOS - UI - UISlider
  8. 关于VIM统计命令
  9. 新Mac 开机启动MySQL/MongoDB/Redis 等服务
  10. 201521123075 《Java程序设计》第5周学习总结
  11. Asp.Net Web API(三)
  12. C++之Binary Heap/Max Heap
  13. easyui datagrid使用按钮
  14. 对于mysql数据库优化的见解
  15. SSM的 日常错误 之 mybatis
  16. Nunit单元测试入门学习随笔(一)
  17. 坑之mysql 字符串与数字操作
  18. (转)PHP5使用cookie时报错 cannot modify header information - headers already sent by (......)
  19. Linux下查看端口,强制kill进程
  20. 【bzoj2961】 共点圆

热门文章

  1. TextView之一:子类的常用属性 分类: H1_ANDROID 2013-10-30 15:14 770人阅读 评论(0) 收藏
  2. wdcp忘记mysql的root密码
  3. 【机器学习实战】第7章 集成方法(随机森林和 AdaBoost)
  4. JavaEE分层知识点粗略解释
  5. one hot 编码的实现
  6. 20171028洛谷NOIP模拟
  7. 使用Boolean类型同步锁引起异常的分析
  8. 混合使用C++语言和Objective-C语言
  9. SpringBoot JPA 专题
  10. HDU1074 Doing Home Work - 状压dp