用途说明

  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已经修改成下面的样子。

C代码  
  1. /* tailf.c -- tail a log file and then follow it
  2. * Created: Tue Jan  9 15:49:21 1996 by faith@acm.org
  3. * Copyright 1996, 2003 Rickard E. Faith (faith@acm.org)
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * less -F and tail -f cause a disk access every five seconds.  This
  24. * program avoids this problem by waiting for the file size to change.
  25. * Hence, the file is not accessed, and the access time does not need to be
  26. * flushed back to disk.  This is sort of a "stealth" tail.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <unistd.h>
  31. #include <malloc.h>
  32. #include <sys/stat.h>
  33. //#include "nls.h"
  34. #define _(s) s
  35. static size_t filesize(const char *filename)
  36. {
  37. struct stat sb;
  38. if (!stat(filename, &sb)) return sb.st_size;
  39. return 0;
  40. }
  41. static void tailf(const char *filename, int lines)
  42. {
  43. char **buffer;
  44. int  head = 0;
  45. int  tail = 0;
  46. FILE *str;
  47. int  i;
  48. if (!(str = fopen(filename, "r"))) {
  49. fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename);
  50. perror("");
  51. exit(1);
  52. }
  53. buffer = malloc(lines * sizeof(*buffer));
  54. for (i = 0; i < lines; i++) buffer[i] = malloc(BUFSIZ + 1);
  55. while (fgets(buffer[tail], BUFSIZ, str)) {
  56. if (++tail >= lines) {
  57. tail = 0;
  58. head = 1;
  59. }
  60. }
  61. if (head) {
  62. for (i = tail; i < lines; i++) fputs(buffer[i], stdout);
  63. for (i = 0; i < tail; i++)     fputs(buffer[i], stdout);
  64. } else {
  65. for (i = head; i < tail; i++)  fputs(buffer[i], stdout);
  66. }
  67. fflush(stdout);
  68. for (i = 0; i < lines; i++) free(buffer[i]);
  69. free(buffer);
  70. }
  71. int main(int argc, char **argv)
  72. {
  73. char       buffer[BUFSIZ];
  74. size_t     osize, nsize;
  75. FILE       *str;
  76. const char *filename;
  77. int        count;
  78. //setlocale(LC_ALL, "");
  79. //bindtextdomain(PACKAGE, LOCALEDIR);
  80. //textdomain(PACKAGE);
  81. if (argc != 2) {
  82. fprintf(stderr, _("Usage: tailf logfile\n"));
  83. exit(1);
  84. }
  85. filename = argv[1];
  86. tailf(filename, 10);
  87. for (osize = filesize(filename);;) {
  88. nsize = filesize(filename);
  89. if (nsize != osize) {
  90. if (!(str = fopen(filename, "r"))) {
  91. fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename);
  92. perror(argv[0]);
  93. exit(1);
  94. }
  95. if (!fseek(str, osize, SEEK_SET))
  96. while ((count = fread(buffer, 1, sizeof(buffer), str)) > 0)
  97. fwrite(buffer, 1, count, stdout);
  98. fflush(stdout);
  99. fclose(str);
  100. osize = nsize;
  101. }
  102. usleep(250000);     /* 250mS */
  103. }
  104. return 0;
  105. }

[root@smsgw tailf]# gcc -Wall -o /usr/bin/tailf tailf.c 
[root@smsgw tailf]# tailf 
Usage: tailf logfile
[root@smsgw tailf]#

一天一条Linux指令-tailf相关推荐

  1. 一天一条Linux指令-sz

    用途说明 sz命令是利用ZModem协议来从Linux服务器传送文件到本地,一次可以传送一个或多个文件.相对应的从本地上传文件到Linux服务器,可以使用rz命令.参见<我使用过的Linux命令 ...

  2. 一天一条Linux指令-clear

    用途说明 clear命令是用来清除终端屏幕的(clear the terminal screen),使用过DOS或者Windows的cmd的人知道有个cls命令,可以用来清除屏幕,但Linux底下没有 ...

  3. centos7修改ip地址命令_每天一条Linux命令(12) hostnam (超详细)

    在Linux系统中,命令 hostname 用于显示或者设置系统主机名称.许多网络程序均用主机名来标识主机,若没有设置好主机名,则可能会导致网络服务不正常. 语法: hostname [选择] 参数: ...

  4. linux 复制文件到另一个目录命令_每天一条Linux命令(21) scp (远程文件复制)

    数据与编程之美 在Linux系统中scp用于在不同的主机之间复制文件,它采用SSH协议来保证复制的安全性.scp命令每次都是全量完整复制,因此效率不高,适合第一次复制时使用,增量复制建议使用rsync ...

  5. linux history命令显示时间_每天一条Linux命令(29) more (分页显示内容)

    在上一篇中我们学习了命令 less ,其作用是以分页的形式查看文件内容并可以对内容进行交互式操作.今天我们学习到的是命令 more,从字面意思乍一看命令less与命令more恰好为互反的两条命令,但其 ...

  6. unzip 解压_每天一条Linux命令(11) unzip (超详细)

    在Linux系统中,命令 unzip 用于解压zip命令或其他压缩软件压缩的zip格式文件. 语法: unzip [选项] [压缩文件] 常用参数说明: -o  解压时不提示是否覆盖文件 -v 解压时 ...

  7. 每天一条Linux命令(23) host (域名查询工具)

    在Linux系统中,命令 host 用于查询DNS的工具,他可以将指定主机名称转换为IP地址. 什么又是NDS? DNS(Domain Name System)翻译为域名解析系统,作用是将域名解析为机 ...

  8. linux登录指令 pgsql_一句一例解读20条Linux常用指令,学会了你就入门了

    玩过Linux的朋友都知道,Linux是基于命令行的操作系统,学习Linux必须学习Linux的指令,但是Linux的指令群非常庞大,很多"小白"无从下手,作者根据多年授课经验,总 ...

  9. Linux 指令简单将***IP列入iptables 限制范围

    Linux 指令简单将***IP列入iptables 限制范围 今天部分服务器收到真IP的非SYN***,郁闷. netstat -an | grep -v LISTEN | awk '{print ...

最新文章

  1. 接口或抽象类:使用哪一个?
  2. 010_JavaScript变量
  3. 蓝卡在哪里_什么是蓝卡,魅力在哪里,让申请者为之着迷?
  4. numpy 平方_Numpy的终极备忘录
  5. 多个域名要选择合适的SSL证书
  6. IMAP与POP3的比较
  7. js 正则 或_一次记住js的6个正则方法
  8. 比较热门好用的开源中文分词软件系统有哪些?
  9. 运营小技能:订阅号文章排版教程(添加图片超链接、推文采集、往期推荐)
  10. 高等数学——多元函数极值的定义
  11. 正态分布、指数分布的特征函数及期望与方差 - 随机过程
  12. 家电电子行业内卷严重,你还觉得单片机没用吗
  13. android 触摸事件流程。
  14. 如何系统的学习linux
  15. ChatGPT专业应用:撰写节日营销活动方案
  16. 模拟电路笔记(三)放大器
  17. GBase基本查询操作
  18. Discuz提速优化技巧
  19. 局域网可以访问,但打印机就是连不上,提示拒绝访问,原来是这个原因
  20. 1张上海各阶层本科生真实工资表流出,戳穿了对年轻人最残忍的骗局

热门文章

  1. Spark学习-入门介绍
  2. Python uniform() 函数
  3. 微信分享小功能后端接口简单实现
  4. 多多自走棋改动_多多自走棋:几个隐藏更新,体验服公告真没提,有一项熟悉又隐蔽...
  5. 二叉树讲解《三》(堆的应用)
  6. JavaScript 正则表达式匹配汉字
  7. Caddy 源码阅读
  8. 区块链对抑制公共部门的腐败有什么作用?
  9. 51单片机 蜂鸣器播放提示音
  10. Ubuntu系统在终端查看一些软硬件版本或文件大小的指令!