linux进程操作日志文件,我使用过的Linux命令之tailf - 跟踪日志文件/更好的tail -f版本...
用途说明
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代码
/* 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
#include
#include
#include
#include
//#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
while (fgets(buffer[tail], BUFSIZ, str)) {
if (++tail >= lines) {
tail = 0;
head = 1;
}
}
if (head) {
for (i = tail; i
for (i = 0; i
} else {
for (i = head; i
}
fflush(stdout);
for (i = 0; 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]#
下载次数: 16
linux进程操作日志文件,我使用过的Linux命令之tailf - 跟踪日志文件/更好的tail -f版本...相关推荐
- Linux下好用的日志库,我使用過的Linux命令之tailf - 跟蹤日志文件/更好的tail -f版本...
用途說明 tailf命令幾乎等同於tail -f,嚴格說來應該與tail --follow=name更相似些.當文件改名之后它也能繼續跟蹤,特別適合於日志文件的跟蹤(follow the growth ...
- linux进程操作相关函数,Linux进程控制简介与要素及相关函数详解
进程是操作系统中的一个重要概念,它是一个程序的一次执行过程,程序是进程的一种静态描述,系统中运行的每一个程序都是在它的进程中运行的. 进程4要素 要有一段程序供该进程运行 进程专用的系统堆栈空间 进程 ...
- Linux进程描述符task_struct结构体详解--Linux进程的管理与调度(一)
转自:http://blog.csdn.net/gatieme/article/details/51383272 日期 内核版本 架构 作者 GitHub CSDN 2016-05-12 Linux- ...
- linux 进程监控 工具,你值得拥有:25个Linux性能监控工具
一段时间以来,我们在网上向读者介绍了如何为Linux以及类Linux操作系统配置多种不同的性能监控工具.在这篇文章中我们将罗列一系列使用最频繁的性能监控工具,并对介绍到的每一个工具提供了相应的简介链接 ...
- centos 服务器传文件,CentOS下怎么用SCP命令进行服务器之间的文件传输?
SCP命令用于Linux系统(CENTOS)之间复制文件和目录操作. 参数说明: -1: 强制scp命令使用协议ssh1 -2: 强制scp命令使用协议ssh2 -4: 强制scp命令只使用IPv4寻 ...
- linux进程管理 pdf,高效与精细的结合--Linux的进程管理.pdf
高效与精细的结合--Linux的进程管理.pdf 第 卷 第 期 A 文献标识码 I T6L 76 28 L J6 7 8 676 LJ Q Q656 8J6 6 82 K 797863 R28J 2 ...
- linux进程 crash 分析工具,crash工具分析大型Linux服务器死锁实战
Linux服务器背景: CPUS: 40 MEMORY: 127.6 GB MACHINE: x86_64 (2199 Mhz) Linux Kernel: 4.4.121 TASKS: 19411 ...
- linux进程监控monit,教你使用monit监控Linux系统
Monit 是用于对系统中的进程.文件.目录.以及设备等进行监视和管理的工具.当你所指定的server宕机或者没有反应,monit会将该进程杀死并重启该server.并通过邮件进行通知.Monit 包 ...
- linux 进程原理内存,linux进程通信之共享内存原理(基于linux 1.2.13)
1 有一个全局的结构体数据,每次需要一块共享的内存时(shmget),从里面取一个结构体,记录相关的信息. struct shmid_ds { // 权限相关 struct ipc_perm shm_ ...
- linux 进程复活,复活意义何在:QQ for Linux 新版测试
QQ,一个承载了太多国人记忆的聊天软件,也是一个承载了 Linux 用户太多纠结的聊天软件.薄荷君自 2004 年接触 Linux 开始,如何在 Linux 上运行 QQ,就是一个经久不衰的话题.开始 ...
最新文章
- ISE 14.7 调试错误笔记
- 博途v15安装过程中提示出错_博途V15.1对应的V90 HSP和GSD文件安装
- noip2019集训测试赛(五)
- poj3687Labeling Balls
- Fluid 0.5 版本发布:开启数据集缓存在线弹性扩缩容之路
- 使用程序创建数据库表
- 不用第三方库,也能用 Python 作图,效果还不错
- Java Web整合开发(26) -- Spring概述
- python爬取文件归类_python爬取各类文档方法归类汇总
- 3分钟学会python_3分钟学会使用Python推荐系统库Surprise
- Cache 和 Buffer 有什么区别
- 大漠为什么不支持win10_Win10系统注册使用大漠插件的方法与设置!常见错误0x8002801...
- 大众点评 java_大众点评评分爬取-图文识别ORC
- 如何提取网易公开课的音频
- 基于GitLab+Docker+K8S的持续集成和交付
- 新浪财经独家对话达利欧:桥水员工离职率曾高达30%
- 这是我见过最牛逼的Shell脚本!
- 禁止浏览器对页面进行缩放
- php禁用gopher协议,SSRF攻击-运用gopher协议构造POST包--emmmm(http://10.112.68.215:10004/index.php?action=login)...
- 【odoo15】由于目标计算机积极拒绝,无法连接。
热门文章
- c语言变量ppt,C语言数据类型和变量课件PPT
- toolchain安装教程支持_Ubuntu安装ARM架构GCC工具链(ubuntu install ARM toolchain)最简单办法...
- 圆你导演梦:5款主流视频制作软件横评
- 关键系统进程 C:\Windows\system32\lsass.exe 失败,状态代码是 255。现在必须重新启动计算机。
- java 工作流 详解
- 计算机组装训练知识总结,组装知识总结
- 爱普生Epson L301 清零软件+图解教程
- 超酷计算机病毒,世界上十种最强的计算机病毒,最著名的计算机病毒都在这里!...
- office怎么像wps一样多栏_WPS文档分栏设置,让排版更完美 Word2010如何分两栏、三栏、多栏技巧...
- 科大讯飞 asr 实时语音转写 rtasr 基于Netty编写的websocket client SDK