CentOS添加tailf命令

在目录/usr/local/中添加一个新的文件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 filesizeconst char *filename)  
{  
    struct stat sb;  
  
    if !statfilename, &sb)) return sb.st_size;  
    return 0;  
}  
  
static void tailfconst char *filename, int lines)  
{  
    char **buffer;  
    int  head = 0;  
    int  tail = 0;  
    FILE *str;  
    int  i;  
  
    if !str = fopenfilename, "r"))) {  
    fprintfstderr, _"Cannot open "%s" for read
"), filename);  
    perror"");  
    exit1);  
    }  
  
    buffer = malloclines * sizeof*buffer));  
    for i = 0; i < lines; i++) buffer[i] = mallocBUFSIZ + 1);  
  
    while fgetsbuffer[tail], BUFSIZ, str)) {  
    if ++tail >= lines) {  
        tail = 0;  
        head = 1;  
    }  
    }  
  
    if head) {  
    for i = tail; i < lines; i++) fputsbuffer[i], stdout);  
    for i = 0; i < tail; i++)     fputsbuffer[i], stdout);  
    } else {  
    for i = head; i < tail; i++)  fputsbuffer[i], stdout);  
    }  
    fflushstdout);  
  
    for i = 0; i < lines; i++) freebuffer[i]);  
    freebuffer);  
}  
  
int mainint argc, char **argv)  
{  
    char       buffer[BUFSIZ];  
    size_t     osize, nsize;  
    FILE       *str;  
    const char *filename;  
    int        count;  
  
    //setlocaleLC_ALL, "");  
    //bindtextdomainPACKAGE, LOCALEDIR);  
    //textdomainPACKAGE);  
  
    if argc != 2) {  
    fprintfstderr, _"Usage: tailf logfile
"));  
    exit1);  
    }  
  
    filename = argv[1];  
  
    tailffilename, 10);  
  
    for osize = filesizefilename);;) {  
    nsize = filesizefilename);  
    if nsize != osize) {  
        if !str = fopenfilename, "r"))) {  
        fprintfstderr, _"Cannot open "%s" for read
"), filename);  
        perrorargv[0]);  
        exit1);  
        }  
        if !fseekstr, osize, SEEK_SET))  
                while count = freadbuffer, 1, sizeofbuffer), str)) > 0)  
                    fwritebuffer, 1, count, stdout);  
        fflushstdout);  
        fclosestr);  
        osize = nsize;  
    }  
    usleep250000);     /* 250mS */  
    }  
    return 0;  
}

运行一下指令,不报错即生效  gcc -o /usr/bin/tailf tailf.c

若报错,根据报错进行解决

一般的错误原因可能是:

1.gcc未安装

2.路径没有设置正确

归纳总结一下tailf命令

二、tailf-跟随日志文件的增长

语法
tailf [OPTION]文件

描述
tailf将打印出文件的最后10行,然后等待
文件增长。它类似于tail -f,但不访问文件
当它没有增长。这样做的副作用是不更新
文件的访问时间,所以文件系统刷新不会发生periodi?
没有日志活动发生时,显示为cally。

tailf在监视笔记本电脑上的日志文件时非常有用
记录很少,并且用户希望硬盘旋转
以节省电池寿命。

长选项的强制性参数是短选项的强制性参数
太。

-n,–lines = N,-N
输出最后N行,而不是最后10行。

-V,–version
输出版本信息并退出。

-h,–help
显示帮助并退出

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注