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 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;  
}

 

運行一下指令,不報錯即生效  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
顯示幫助並退出

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM