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. opencv和linux的关联

    这是一篇关于opencv和linux关联的文章

  2. iOS开发webView的使用二

    #import "ViewController.h" @interface ViewController ()<UIWebViewDelegate> @property ...

  3. iOS开发之Quartz2D 五:UIKIT 绘图演练,画文字,画图片

    #import "DrawView.h" @implementation DrawView -(void)awakeFromNib { // //画图片 // UIImage *i ...

  4. Windows共享上网的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 现在有这样一个网络应用场景:A机器是一个PC台式机,处在两个网络中,一个是处在192.168.30.1到192.168 ...

  5. ios开发事件处理之:五:事件的响应

  6. Android JNI 自定义对象为参数和返回值

    ndroid JNI 提供了很强大的支持,不仅可以采用基本类型做为参数和返回值,同时也支持自定义对象做为参数和返回值,以下举例说明. 一.定义作为输入和返回的自定义类 (仅提供两个简单类型和一个打印函 ...

  7. php 获取提交来源,判断从哪里提交的

    echo $_SERVER['HTTP_REFERER'];这个获取上个页面的url例如获得的是 $url = http://www.weisuyun.com/nihao.html其他页面提交过来的不 ...

  8. css选择器指定元素中第几个子元素

    tr td:nth-child(2){ background-color:gray; } 就是tr当中的td的第二个td的属性 tr:nth-child(2n+0){ background-color ...

  9. C++生成GIF小结

    声明:所有权利保留. 转载必须说明出处:http://blog.csdn.net/cartzhang/article/details/44020175 近来需要把BMP或Kinect的内存图片多张合成 ...

  10. jsp与servlet(转)

    一.基本概念 1.1 Servlet Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面.它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器 ...