- 浏览: 4732853 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
bzhao:
你也应该可以这样:(not tested)./rbtunnel ...
在Bash脚本中怎么关闭文件描述符? -
bzhao:
如果有A进程原代码情况下,通过如下调用,把他的子进程继承关闭则 ...
在Bash脚本中怎么关闭文件描述符? -
Master-Gao:
楼主咋没分析下源码呢?
我使用过的Linux命令之dirname - 截取给定路径的目录部分 -
jiedushi:
tail -F 就可以吧
Linux下实时跟踪log4j日志文件的bash脚本 - 增强了tail -f的功能 -
java_is_new:
新手学习了,就是不明白为一个网卡配多个ip有什么用
我使用过的Linux命令之ifconfig - 网络配置命令
我使用过的Linux命令之tailf - 跟踪日志文件/更好的tail -f版本
本文链接:http://codingstandards.iteye.com/blog/832760 (转载请注明出处)
用途说明
tailf命令几乎等同于tail -f,严格说来应该与tail --follow=name更相似些。当文件改名之后它也能继续跟踪,特别适合于日志文件的跟踪(follow the growth of a log file)。与tail -f不同的是,如果文件不增长,它不会去访问磁盘文件(It is similar to tail -f but does not access the file when it is not growing. This has the side effect of not updating the access time for the file, so a filesystem flush does not occur periodically when no log activity is happening.)。tailf特别适合那些便携机上跟踪日志文件,因为它能省电,因为减少了磁盘访问嘛(tailf is extremely useful for monitoring log files on a laptop when logging is infrequent and the user desires that the hard disk spin down to conserve battery life.)。tailf命令不是个脚本,而是一个用C代码编译后的二进制执行文件,某些Linux安装之后没有这个命令,本文提供了怎么编译安装tailf命令的方法。
常用参数
格式:tailf logfile
动态跟踪日志文件logfile,最初的时候打印文件的最后10行内容。
使用示例
示例一 使用tailf命令跟踪日志
[root@web imx_server]# tailf log/WEB.LOG
"seq": 5710,
"clientId": 1291343201649254,
"presenceStatus": "1"
} }})
16:09:21.324 DEBUG http-80-79 hyjc.cometd.CometdServlet - { "cid": 1291343201649002, "op": "recv", "ncc0": 175}
16:09:21.444 DEBUG http-80-32 hyjc.filter.AccessCounterFilter - uri=/imx/cometd/service
16:09:21.506 DEBUG http-80-79 hyjc.filter.AccessCounterFilter - uri=/imx/cometd/service
16:09:23.239 DEBUG http-80-45 hyjc.cometd.CometdServlet - { "cid": 1291343201649184, "op": "recv", "ncc0": 55}
16:09:23.327 DEBUG http-80-45 hyjc.filter.AccessCounterFilter - uri=/imx/cometd/service
16:09:24.288 DEBUG http-80-145 hyjc.cometd.CometdServlet - { "cid": 1291283017076175, "op": "recv", "ncc0": 82}
16:09:24.339 DEBUG http-80-81 hyjc.cometd.CometdServlet - { "cid": 1291283017076151, "op": "recv", "ncc0": 142}
16:09:24.360 DEBUG http-80-64 hyjc.cometd.CometdServlet - { "cid": 1291343201649090, "op": "recv", "ncc0": 40}
16:09:24.359 DEBUG http-80-143 hyjc.cometd.CometdServlet - { "cid": 1291283017076176, "op": "recv", "ncc0": 66}
16:09:24.878 DEBUG http-80-145 hyjc.filter.AccessCounterFilter - uri=/imx/cometd/service
16:09:24.892 DEBUG http-80-143 hyjc.filter.AccessCounterFilter - uri=/imx/cometd/service
16:09:24.896 DEBUG http-80-64 hyjc.filter.AccessCounterFilter - uri=/imx/cometd/service
16:09:24.906 DEBUG http-80-81 hyjc.filter.AccessCounterFilter - uri=/imx/cometd/service
16:09:25.095 DEBUG http-80-115 hyjc.filter.AccessCounterFilter - uri=/imx/cometd/service
16:09:25.095 DEBUG http-80-115 hyjc.cometd.CometdServlet - msgType IMX_ACTIVE_TEST msgBody {"clientId":1291343201649002,"presenceStatus":"1","seq":385}
16:09:25.095 INFO http-80-115 imx.client.ImxClient - Tx IMX_ACTIVE_TEST{seq=5711,client_id=1291343201649002,presence_status=1(presence_status_online),}
16:09:25.095 DEBUG http-80-115 hyjc.cometd.CometdServlet - { "cid": 1291343201649002, "op": "send", "sent": 0, "rc": 1, "msg": { "mt": "IMX_ACTIVE_TEST", "mb": {
"seq": 5711,
"clientId": 1291343201649002,
"presenceStatus": "1"
} }}
Ctrl+C
[root@web imx_server]#
示例二 编译安装tailf命令
有些Linux版本不带tailf命令的。
[root@smsgw root]# tailf
-bash: tailf: command not found
[root@smsgw root]#
到代码搜索网站www.koders.com输入tailf.c就可以搜索到源代码
tailf.c结果页面:http://www.koders.com/default.aspx?s=tailf.c&submit=Search&la=*&li=*
tailf.c源码页面:http://www.koders.com/c/fidB6EFAC156A7B4C4A46B38039C79B4AD34939EED0.aspx?s=tailf#L1
点左上角的download就可下载,也可直接下载本文附件tailf.zip。
[root@smsgw tailf]# unzip tailf.zip
Archive: tailf.zip
inflating: tailf.c
[root@smsgw tailf]# ls
tailf.c tailf.zip
[root@smsgw tailf]# gcc -o /usr/bin/tailf tailf.c
tailf.c:34:17: nls.h: 没有那个文件或目录
tailf.c: In function `tailf':
tailf.c:53: warning: passing arg 2 of `fprintf' makes pointer from integer without a cast
tailf.c: In function `main':
tailf.c:88: `LC_ALL' undeclared (first use in this function)
tailf.c:88: (Each undeclared identifier is reported only once
tailf.c:88: for each function it appears in.)
tailf.c:89: `PACKAGE' undeclared (first use in this function)
tailf.c:89: `LOCALEDIR' undeclared (first use in this function)
tailf.c:93: warning: passing arg 2 of `fprintf' makes pointer from integer without a cast
tailf.c:105: warning: passing arg 2 of `fprintf' makes pointer from integer without a cast
[root@smsgw tailf]#
修改一下tailf.c的源代码。
第34行附近:注释掉头文件,增加宏定义
//#include "nls.h"
#define _(s) s
第89行附近:把原来的代码注释掉
//setlocale(LC_ALL, "");
//bindtextdomain(PACKAGE, LOCALEDIR);
//textdomain(PACKAGE);
看了源代码之后,你是不是发现其实Linux命令其实并不太神秘。
注:本文附件中的tailf.c已经修改成下面的样子。
/* tailf.c -- tail a log file and then follow it * Created: Tue Jan 9 15:49:21 1996 by faith@acm.org * Copyright 1996, 2003 Rickard E. Faith (faith@acm.org) * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. * * less -F and tail -f cause a disk access every five seconds. This * program avoids this problem by waiting for the file size to change. * Hence, the file is not accessed, and the access time does not need to be * flushed back to disk. This is sort of a "stealth" tail. */ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <malloc.h> #include <sys/stat.h> //#include "nls.h" #define _(s) s static size_t filesize(const char *filename) { struct stat sb; if (!stat(filename, &sb)) return sb.st_size; return 0; } static void tailf(const char *filename, int lines) { char **buffer; int head = 0; int tail = 0; FILE *str; int i; if (!(str = fopen(filename, "r"))) { fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename); perror(""); exit(1); } buffer = malloc(lines * sizeof(*buffer)); for (i = 0; i < lines; i++) buffer[i] = malloc(BUFSIZ + 1); while (fgets(buffer[tail], BUFSIZ, str)) { if (++tail >= lines) { tail = 0; head = 1; } } if (head) { for (i = tail; i < lines; i++) fputs(buffer[i], stdout); for (i = 0; i < tail; i++) fputs(buffer[i], stdout); } else { for (i = head; i < tail; i++) fputs(buffer[i], stdout); } fflush(stdout); for (i = 0; i < lines; i++) free(buffer[i]); free(buffer); } int main(int argc, char **argv) { char buffer[BUFSIZ]; size_t osize, nsize; FILE *str; const char *filename; int count; //setlocale(LC_ALL, ""); //bindtextdomain(PACKAGE, LOCALEDIR); //textdomain(PACKAGE); if (argc != 2) { fprintf(stderr, _("Usage: tailf logfile\n")); exit(1); } filename = argv[1]; tailf(filename, 10); for (osize = filesize(filename);;) { nsize = filesize(filename); if (nsize != osize) { if (!(str = fopen(filename, "r"))) { fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename); perror(argv[0]); exit(1); } if (!fseek(str, osize, SEEK_SET)) while ((count = fread(buffer, 1, sizeof(buffer), str)) > 0) fwrite(buffer, 1, count, stdout); fflush(stdout); fclose(str); osize = nsize; } usleep(250000); /* 250mS */ } return 0; }
[root@smsgw tailf]# gcc -Wall -o /usr/bin/tailf tailf.c
[root@smsgw tailf]# tailf
Usage: tailf logfile
[root@smsgw tailf]#
问题思考
相关资料
【1】linuxcommand.org tailf手册页
【2】代码搜索 tailf源代码
- tailf.zip (3.4 KB)
- 下载次数: 42
发表评论
-
在Linux用tar归档压缩文件时忽略某些文件和目录
2013-02-01 10:19 16927在Linux下,常用tar对文 ... -
使用nmap扫描服务器端口的一次操作
2012-11-01 17:00 15075使用nmap扫描服务器端口的一次操作 本文来自:http ... -
我使用过的Linux命令之wget - ooo
2011-09-14 13:10 0我使用过的Linux命令之wg ... -
推荐一篇学习Vim使用的好文:酷壳 - 简明 Vim 练级攻略
2011-09-09 12:53 9085简明 Vim 练级攻略 http://coolshell.c ... -
推荐一篇学习Vim使用的好文:酷壳 - 简明 Vim 练级攻略
2011-09-09 12:49 1简明 Vim 练级攻略 http://coolshell.c ... -
我使用过的Linux命令之:(冒号) - 啥也不做(除了……)
2011-08-29 13:18 11984我使用过的Linux命令之: ... -
我使用过的Linux命令之date - 显示、修改系统日期时间
2011-08-25 09:21 41905我使用过的Linux命令之da ... -
我使用过的Linux命令之declare - 声明shell变量(不知道没关系、知道了就更好的内建命令)
2011-08-16 09:22 21741我使用过的Linux命令之declare - 声明shell变 ... -
我使用过的Linux命令之alias - 设置命令的别名,让 Linux 命令更简练
2011-08-11 09:31 28717我使用过的Linux命令之alias - 设置命令的别名,让 ... -
我使用过的Linux命令之ar - 创建静态库.a文件
2011-08-08 10:40 51816我使用过的Linux命令之ar - 创建静态库.a文件 本 ... -
我使用过的Linux命令之crontab - 设置例行任务(类似于Windows中的任务计划)
2011-08-04 22:26 9692我使用过的Linux命令之crontab - 设置例行任务(类 ... -
我使用过的Linux命令之chmod - 改变文件/目录的访问权限
2011-08-03 21:33 10641我使用过的Linux命令之chmod - 改变文件/目录的访问 ... -
我使用过的Linux命令之export - 设置或显示环境变量
2011-08-02 19:55 25371我使用过的Linux命令之export - 设置或显示环境变量 ... -
我使用过的Linux命令之wc - 统计文件行数、单词数或字节数
2011-07-26 10:50 28933我使用过的Linux命令之wc - 统计文件行数、单词数或字节 ... -
我使用过的Linux命令之groupdel - 删除群组
2011-07-22 22:13 9620我使用过的Linux命令之gr ... -
我使用过的Linux命令之ifconfig - 网络配置命令
2011-07-21 20:43 60377我使用过的Linux命令之ifconfig - 网络配置命令 ... -
我使用过的Linux命令之ll - 列出文件/目录的详细信息
2011-07-20 21:22 7104我使用过的Linux命令之ll ... -
我使用过的Linux命令之mkdir - 创建目录
2011-07-20 20:58 13605我使用过的Linux命令之mkdir - 创建目录 本文链 ... -
我使用过的Linux命令之perror - 解释错误代码
2011-07-18 20:29 24969我使用过的Linux命令之perror - 解释错误代码 ... -
我使用过的Linux命令之ping - 测试与目标主机的连通性
2011-07-16 10:46 26264我使用过的Linux命令之ping - 测试与目标主机的连通性 ...
相关推荐
tailf命令几乎等同于tail -f,严格说来应该与tail –follow=name更相似些。当文件改名之后它也能继续跟踪,特别适合于日志文件的跟踪(follow the growth of a log file)。与tail -f不同的是,如果文件不增长,它...
Linux操作系统 兄弟连Linux常用命令 1.1命令格式 Linux命令格式 命令 [-选项] [参数] ctrl+l清屏 1.2目录处理命令 1. ls----显示目录文件 ...6、head-tail---显示文件前面几行,或者后几行 1.4链接
很多人喜欢使用tail –f 来监控日志文件。 一、Linux tail命令格式 Linux tail命令 格式如下所示 tail [OPTION]… [FILE]… Linux tail命令 参数如下所示 -f 循环读取 -q 不显示处理信息 -v 显示详细的处理信息 -c&...
日志管理 分为两类 1.rsyslog 系统日志管理 2.logrotate 日志轮转 ...tail -f /var/log/messages //动态查看日志文件的尾部 tailf /var/log/secure //认证、安全 tail /var/log/yum.log //yum tail /var/log/maillog
Linux操作系统基础
laravel-tail, 用于跟踪应用程序日志的工匠命令 轻松跟踪你的日志 这个软件包提供了一个工匠命令来跟踪应用程序日志。 它支持你本地机器上的每日和单一日志。Spatie是比利时Antwerp的网页设计机构。 你将在我们的...
实现window系统下 类似 Linux 命令行 tail -f 功能.使用C#语言开发,占用资源小。如有问题可留言
解压后将tail.exe 复制到目录:C:\Windows\System32 下即可在cmd里使用tail命令。 解决windows无法使用linux的经典工具.
它充当“tail -f”linux 命令。 它有助于跟踪可通过 http 访问的日志。 安装 脚本需要安装curl 。 许多 Linux 发行版以及 Mac OS X 已经安装了 curl。 在 Ubuntu 上你可以使用这个命令来安装 curl: sudo apt-get ...
1、cat命令 描述:查看文件内容;文件创建;文件合并。常用来显示文件内容,或者将几个文件连接起来显示,或者从标准输入读取内容并显示,它常与重定向符号配合使用。 用法:cat [参数选项] [文件名称] 参数: 参数 ...
linux分为:redhat linux系列和debian系列。 X Window是一种协议。 unix图形环境是CDE:common desktop environment通用桌面环境。 linux系统结构:硬件、内核层、shell层、应用层、用户。 在安装linux的时候,会...
linux tail 命令 参数 用法 自己留用
windows下使用tail命令-tail2win 下载之后解压到c:/windows/system32目录下 然后就可以像linux那样使用tail -f 指令
这个是开源的tail工具,有界面的,运行于windows环境。纯鼠标操作,还可以设置关键字过虑与高亮,在日志里面如果出现异常日志时,就可以通过这个功能只显示有问题的行。而不需要被其它正常日志信息阻碍视线
Linux网络操作系统基础:文件命令读取文件头尾命令head和tail.pptx
使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新,使你看到最新的文件内容.接下来通过本文给大家介绍PHP实现linux命令tail -f,需要的...
用法$ tail -f ./storage/logs/laravel.log [2017-09-05 14:52:14] local.DEBUG: [800μs] select count(*) as aggregate from `discussions` where `discussions`.`deleted_at` is null | GET: ...
、基本命令 1.查看/etc/passwd的前10行; head -10 /etc/passwd 2.查看/etc/man.config的后10行并将输出结果保存到/root/exam/osta.txt ; tail -10 /etc/man.config> /root/exam/osta.txt 3.查看系统的硬盘和内存的...
linux中tail 命令使用详解.docx