Centos tailf command

Source: Internet
Author: User

Tailf of Linux commands I have used-tracking log files/better tail-f versions

Description

The tailf command is almost equivalent to the tail-f command. It should be more similar to the tail -- follow = name command. After a file is renamed, it can also continue tracking, which is particularly suitable for tracking log files (follow the growth of a log file ). Unlike tail-f, if the file does not grow, It will not access the disk file (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 is especially suitable for tracking log files on hosts because it can save power, because it reduces disk access (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 .). The tailf command is not a script, but a binary execution file compiled with C code. Some Linux instances do not have this command after installation. This article provides a method for compiling and installing the tailf command.

Common Parameters

Format: tailf logfile

The logfile of the log file is dynamically tracked, and the last 10 lines of the file are printed at the beginning.

Example 1 use the tailf command to track logs

[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. 095debug 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] #

Example 2 compile and install the tailf command

Some Linux versions do not contain the tailf command.

[Root @ smsgw root] # tailf
-Bash: tailf: command not found
[Root @ smsgw root] #

Go to the code search website www.koders.com and enter tailf. c to search for the source code.

Tailf. c result page: http://www.koders.com/default.aspx? S = tailf. c & submit = Search & la = * & li = *

Tailf. c source code page: http://www.koders.com/c/fidB6EFAC156A7B4C4A46B38039C79B4AD34939EED0.aspx? S = tailf # L1

Click downloadat the upper left corner to download the file, or directly download the attachment 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. c
Tailf. c: 34: 17: nls. h: No file or directory
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] #

Modify the source code of tailf. c.

Near line 1: comment out the U-turn file and add macro definition

// # Include "nls. h"
# Define _ (s) s

Near line 1: comment out the original code

// Setlocale (LC_ALL ,"");
// Bindtextdomain (PACKAGE, LOCALEDIR );
// Textdomain (PACKAGE );

After reading the source code, do you find that Linux commands are actually not too mysterious.

Note: The tailf. c in the attachment has been changed to the following.

C code

/* 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. c
[Root @ smsgw tailf] # tailf
Usage: tailf logfile
[Root @ smsgw tailf] #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.