| 您的当前位置:首页 --> CentOS入门 |
| Centos tailf命令 |
| 浏览次数:5697 关键词 ( 命令 Centos tailf ) |
|
我使用过的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 示例二 编译安装tailf命令 有些Linux版本不带tailf命令的。
[root@smsgw root]# tailf 到代码搜索网站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 修改一下tailf.c的源代码。 第34行附近:注释掉头文件,增加宏定义
//#include "nls.h" 第89行附近:把原来的代码注释掉
//setlocale(LC_ALL, ""); 看了源代码之后,你是不是发现其实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 <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 |
| 下载次数:28 |
| 下载地址:点击下载 |
| 本资源为程序自动采集,如有侵权请联系我们移除 admin#80vps.com 来信请将#替换为@ |
| 下一条 centos系统中网络配置相关 上一条 centos5.6挂载6T的空间 |