我使用过的Linux命令之tailf - 跟踪日志文件/更好的tail -f版本

2024-08-27 11:58

本文主要是介绍我使用过的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已经修改成下面的样子。

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.   
  29. #include <stdio.h>  
  30. #include <stdlib.h>  
  31. #include <unistd.h>  
  32. #include <malloc.h>  
  33. #include <sys/stat.h>  
  34. //#include "nls.h"  
  35. #define _(s) s  
  36.   
  37. static size_t filesize(const char *filename)  
  38. {  
  39.     struct stat sb;  
  40.   
  41.     if (!stat(filename, &sb)) return sb.st_size;  
  42.     return 0;  
  43. }  
  44.   
  45. static void tailf(const char *filename, int lines)  
  46. {  
  47.     char **buffer;  
  48.     int  head = 0;  
  49.     int  tail = 0;  
  50.     FILE *str;  
  51.     int  i;  
  52.   
  53.     if (!(str = fopen(filename, "r"))) {  
  54.     fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename);  
  55.     perror("");  
  56.     exit(1);  
  57.     }  
  58.   
  59.     buffer = malloc(lines * sizeof(*buffer));  
  60.     for (i = 0; i < lines; i++) buffer[i] = malloc(BUFSIZ + 1);  
  61.   
  62.     while (fgets(buffer[tail], BUFSIZ, str)) {  
  63.     if (++tail >= lines) {  
  64.         tail = 0;  
  65.         head = 1;  
  66.     }  
  67.     }  
  68.   
  69.     if (head) {  
  70.     for (i = tail; i < lines; i++) fputs(buffer[i], stdout);  
  71.     for (i = 0; i < tail; i++)     fputs(buffer[i], stdout);  
  72.     } else {  
  73.     for (i = head; i < tail; i++)  fputs(buffer[i], stdout);  
  74.     }  
  75.     fflush(stdout);  
  76.   
  77.     for (i = 0; i < lines; i++) free(buffer[i]);  
  78.     free(buffer);  
  79. }  
  80.   
  81. int main(int argc, char **argv)  
  82. {  
  83.     char       buffer[BUFSIZ];  
  84.     size_t     osize, nsize;  
  85.     FILE       *str;  
  86.     const char *filename;  
  87.     int        count;  
  88.   
  89.     //setlocale(LC_ALL, "");  
  90.     //bindtextdomain(PACKAGE, LOCALEDIR);  
  91.     //textdomain(PACKAGE);  
  92.   
  93.     if (argc != 2) {  
  94.     fprintf(stderr, _("Usage: tailf logfile\n"));  
  95.     exit(1);  
  96.     }  
  97.   
  98.     filename = argv[1];  
  99.   
  100.     tailf(filename, 10);  
  101.   
  102.     for (osize = filesize(filename);;) {  
  103.     nsize = filesize(filename);  
  104.     if (nsize != osize) {  
  105.         if (!(str = fopen(filename, "r"))) {  
  106.         fprintf(stderr, _("Cannot open \"%s\" for read\n"), filename);  
  107.         perror(argv[0]);  
  108.         exit(1);  
  109.         }  
  110.         if (!fseek(str, osize, SEEK_SET))  
  111.                 while ((count = fread(buffer, 1, sizeof(buffer), str)) > 0)  
  112.                     fwrite(buffer, 1, count, stdout);  
  113.         fflush(stdout);  
  114.         fclose(str);  
  115.         osize = nsize;  
  116.     }  
  117.     usleep(250000);     /* 250mS */  
  118.     }  
  119.     return 0;  
  120. }  
  121.    
 

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


  • tailf.zip (3.4 KB)
  • 下载次数: 16

这篇关于我使用过的Linux命令之tailf - 跟踪日志文件/更好的tail -f版本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1111607

相关文章

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

Linux join命令的使用及说明

《Linuxjoin命令的使用及说明》`join`命令用于在Linux中按字段将两个文件进行连接,类似于SQL的JOIN,它需要两个文件按用于匹配的字段排序,并且第一个文件的换行符必须是LF,`jo... 目录一. 基本语法二. 数据准备三. 指定文件的连接key四.-a输出指定文件的所有行五.-o指定输出

Linux jq命令的使用解读

《Linuxjq命令的使用解读》jq是一个强大的命令行工具,用于处理JSON数据,它可以用来查看、过滤、修改、格式化JSON数据,通过使用各种选项和过滤器,可以实现复杂的JSON处理任务... 目录一. 简介二. 选项2.1.2.2-c2.3-r2.4-R三. 字段提取3.1 普通字段3.2 数组字段四.

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

k8s按需创建PV和使用PVC详解

《k8s按需创建PV和使用PVC详解》Kubernetes中,PV和PVC用于管理持久存储,StorageClass实现动态PV分配,PVC声明存储需求并绑定PV,通过kubectl验证状态,注意回收... 目录1.按需创建 PV(使用 StorageClass)创建 StorageClass2.创建 PV

Python版本与package版本兼容性检查方法总结

《Python版本与package版本兼容性检查方法总结》:本文主要介绍Python版本与package版本兼容性检查方法的相关资料,文中提供四种检查方法,分别是pip查询、conda管理、PyP... 目录引言为什么会出现兼容性问题方法一:用 pip 官方命令查询可用版本方法二:conda 管理包环境方法

Redis 基本数据类型和使用详解

《Redis基本数据类型和使用详解》String是Redis最基本的数据类型,一个键对应一个值,它的功能十分强大,可以存储字符串、整数、浮点数等多种数据格式,本文给大家介绍Redis基本数据类型和... 目录一、Redis 入门介绍二、Redis 的五大基本数据类型2.1 String 类型2.2 Hash

Linux云服务器手动配置DNS的方法步骤

《Linux云服务器手动配置DNS的方法步骤》在Linux云服务器上手动配置DNS(域名系统)是确保服务器能够正常解析域名的重要步骤,以下是详细的配置方法,包括系统文件的修改和常见问题的解决方案,需要... 目录1. 为什么需要手动配置 DNS?2. 手动配置 DNS 的方法方法 1:修改 /etc/res