Linux之tail命令实时收集[纠正误解]
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命令实时收集[纠正误解]的更多相关文章
- Linux的watch命令 — 实时监测命令的运行结果
Linux的watch命令 — 实时监测命令的运行结果 watch 是一个非常实用的命令,基本所有的 Linux 发行版都带有这个小工具,如同名字一样,watch 可以帮你监测一个命令的运行结果,省得 ...
- Linux下tail命令的使用方法
Linux下tail命令的使用方法: linux tail命令用途是依照要求将指定的文件的最后部分输出到标准设备,通常是终端,通俗讲来,就是把某个档案文件的最后几行显示到终端上,假设该档案有更新,ta ...
- 【linux】tail 命令详解
转自:https://www.cnblogs.com/fps2tao/p/7698224.html Linux命令:显示文件结尾 Head/Tail head 与 tail 就像它的名字一样的浅显易懂 ...
- Linux head/tail命令详解
head命令用于显示文件的开头的内容.在默认情况下,head命令显示文件的头10行内容. tail命令用于显示文件的结尾的内容.在默认情况下,taild命令显示文件的后10行内容. head常见命令参 ...
- Linux下tail命令
简述 tail命令从指定点开始将文件写到标准输出,使用tail命令的“-f”选项可以方便的查阅正在改变的日志文件,“tail -f filename”会把filename里最尾部的内容显示在屏幕上,并 ...
- Linux 下 tail 命令
简述 tail命令从指定点开始将文件写到标准输出,使用tail命令的“-f”选项可以方便的查阅正在改变的日志文件,“tail -f filename”会把filename里最尾部的内容显示在屏幕上,并 ...
- 【Linux】tail命令
用途 tail命令主要用于取出后边几行 全称 tail命令的全称即为tail(尾巴) 参数 -n :后边接数字,代表显示几行的意思 -f :循环读取 -q :不显示处理信息 -v :显示详细的处理信息 ...
- Linux的tail命令查看文件
小文件一般用cat 查看,但是如果文件内容过多,用cat就不合适了 可以用tail命令 # 默认显示文件最后十行 tail a.txt # 监视文件的尾部内容,默认十行, 可以-n 20显示20行 ...
- 使用tail命令实时查看日志文件
[Shell] 纯文本查看 复制代码 ? 1 tail -f /日志文件 好了.就这样用.简单吧 退出ctrl+C
随机推荐
- C运行时库(C Run-time Library)详解(提供的另一个最重要的功能是为应用程序添加启动函数。Visual C++对控制台程序默认使用单线程的静态链接库,而MFC中的CFile类已暗藏了多线程)
一.什么是C运行时库 1)C运行时库就是 C run-time library,是 C 而非 C++ 语言世界的概念:取这个名字就是因为你的 C 程序运行时需要这些库中的函数. 2)C 语言是所谓的“ ...
- Qt5 编译 & 打包依赖dll发布
十年前学C++的时候,无聊到把windows 文件夹下几乎所有的*.dll 都看过一遍.偶尔在程序运行时看到缺少 *.dll 的提示,都会直接找出来解决. 随着“开发平台”和“编译器”版本的逐年升级, ...
- js进阶 11-9/10/11 jquery创建和插入节点
js进阶 11-9/10/11 jquery创建和插入节点 一.总结 一句话总结: 1.jquery插入节点8个方法? 内部之前,内部之后,之前,之后:各两个 append()和appendTo() ...
- Smarty3.1.8 安装
应用环境:Winsows7 IIS + PHP5.5.12 + Smarty3.1.8 1. IIS 及 PHP 安装,参照<php手册>,这里不做细表. 2. 假定应用目录为 C:\in ...
- POJ1659Frogs' Neighborhood(lavel定理)
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 7260 Accepted: 31 ...
- html5-3 html5标签(热点地图如何实现)(边学边做)
html5-3 html5标签(热点地图如何实现)(边学边做) 一.总结 一句话总结:热点地图用绝对定位实现. 1.自定义列表怎么弄? dl 自定义列表dt 自定义标题dd 自定义列表内容 2. ...
- Android中再按一次退出实现
很多应用中都有一个在用户后退的时候显示"再按一次退出"的提醒,这个怎么实现呢?有两种方式 第一种方式(最常用) long waitTime = 2000; long touchTi ...
- 小强的HTML5移动开发之路(36)——jQuery中的DOM操作
1.查询 利用选择器查找节点 使用 html() / text() / attr() 输出节点文本和属性值. 注意:下拉列表使用 val() <html> <head> < ...
- TensorFlow 学习(十)—— 工具函数
1. 基本 tf.clip_by_value() 截断,常和对数函数结合使用 # 计算交叉熵 crose_ent = -tf.reduce_mean(tf.log(y_*tf.clip_by_valu ...
- Cocos2d-x 3.1.1 Lua演示样例 ActionManagerTest(动作管理)
Cocos2d-x 3.1.1 Lua演示样例 ActionManagerTest(动作管理) 本篇博客介绍Cocos2d-x的动作管理样例,这个样例展示了Cocos2d-x的几个动作: MoveTo ...