|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们!我利用过的Linux下令之tailf-跟踪日记文件/更好的tail-f版本
用处申明
tailf下令几近同等于tail-f,严厉说来应当与tail--follow=name更类似些。当文件更名以后它也能持续跟踪,出格合适于日记文件的跟踪(followthegrowthofalogfile)。与tail-f分歧的是,假如文件不增加,它不会往会见磁盘文件(Itissimilartotail-fbutdoesnotaccessthefilewhenitisnotgrowing.Thishasthesideeffectofnotupdatingtheaccesstimeforthefile,soafilesystemflushdoesnotoccurperiodicallywhennologactivityishappening.)。tailf出格合适那些便携机上跟踪日记文件,由于它能省电,由于削减了磁盘会见嘛(tailfisextremelyusefulformonitoringlogfilesonalaptopwhenloggingisinfrequentandtheuserdesiresthattheharddiskspindowntoconservebatterylife.)。tailf下令不是个剧本,而是一个用C代码编译后的二进制实行文件,某些Linux安装以后没有这个下令,本文供应了怎样编译安装tailf下令的***。
经常使用参数
格局:tailflogfile
静态跟踪日记文件logfile,最后的时分打印文件的最初10行内容。
利用示例
示例一利用tailf下令跟踪日记
[root@webimx_server]#tailflog/WEB.LOG
"seq":5710,
"clientId":1291343201649254,
"presenceStatus":"1"
}}})
16:09:21.324DEBUGhttp-80-79hyjc.cometd.CometdServlet-{"cid":1291343201649002,"op":"recv","ncc0":175}
16:09:21.444DEBUGhttp-80-32hyjc.filter.AccessCounterFilter-uri=/imx/cometd/service
16:09:21.506DEBUGhttp-80-79hyjc.filter.AccessCounterFilter-uri=/imx/cometd/service
16:09:23.239DEBUGhttp-80-45hyjc.cometd.CometdServlet-{"cid":1291343201649184,"op":"recv","ncc0":55}
16:09:23.327DEBUGhttp-80-45hyjc.filter.AccessCounterFilter-uri=/imx/cometd/service
16:09:24.288DEBUGhttp-80-145hyjc.cometd.CometdServlet-{"cid":1291283017076175,"op":"recv","ncc0":82}
16:09:24.339DEBUGhttp-80-81hyjc.cometd.CometdServlet-{"cid":1291283017076151,"op":"recv","ncc0":142}
16:09:24.360DEBUGhttp-80-64hyjc.cometd.CometdServlet-{"cid":1291343201649090,"op":"recv","ncc0":40}
16:09:24.359DEBUGhttp-80-143hyjc.cometd.CometdServlet-{"cid":1291283017076176,"op":"recv","ncc0":66}
16:09:24.878DEBUGhttp-80-145hyjc.filter.AccessCounterFilter-uri=/imx/cometd/service
16:09:24.892DEBUGhttp-80-143hyjc.filter.AccessCounterFilter-uri=/imx/cometd/service
16:09:24.896DEBUGhttp-80-64hyjc.filter.AccessCounterFilter-uri=/imx/cometd/service
16:09:24.906DEBUGhttp-80-81hyjc.filter.AccessCounterFilter-uri=/imx/cometd/service
16:09:25.095DEBUGhttp-80-115hyjc.filter.AccessCounterFilter-uri=/imx/cometd/service
16:09:25.095DEBUGhttp-80-115hyjc.cometd.CometdServlet-msgTypeIMX_ACTIVE_TESTmsgBody{"clientId":1291343201649002,"presenceStatus":"1","seq":385}
16:09:25.095INFOhttp-80-115imx.client.ImxClient-TxIMX_ACTIVE_TEST{seq=5711,client_id=1291343201649002,presence_status=1(presence_status_online),}
16:09:25.095DEBUGhttp-80-115hyjc.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@webimx_server]#
示例二编译安装tailf下令
有些Linux版本不带tailf下令的。
[root@smsgwroot]#tailf
-bash:tailf:commandnotfound
[root@smsgwroot]#
到代码搜刮网站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@smsgwtailf]#unziptailf.zip
Archive:tailf.zip
inflating:tailf.c
[root@smsgwtailf]#ls
tailf.ctailf.zip
[root@smsgwtailf]#gcc-o/usr/bin/tailftailf.c
tailf.c:34:17:nls.h:没有谁人文件或目次
tailf.c:Infunction`tailf:
tailf.c:53:warning:passingarg2of`fprintfmakespointerfromintegerwithoutacast
tailf.c:Infunction`main:
tailf.c:88:`LC_ALLundeclared(firstuseinthisfunction)
tailf.c:88:(Eachundeclaredidentifierisreportedonlyonce
tailf.c:88:foreachfunctionitappearsin.)
tailf.c:89:`PACKAGEundeclared(firstuseinthisfunction)
tailf.c:89:`LOCALEDIRundeclared(firstuseinthisfunction)
tailf.c:93:warning:passingarg2of`fprintfmakespointerfromintegerwithoutacast
tailf.c:105:warning:passingarg2of`fprintfmakespointerfromintegerwithoutacast
[root@smsgwtailf]#
修正一下tailf.c的源代码。
第34行四周:正文失落头文件,增添宏界说
//#include"nls.h"
#define_(s)s
第89行四周:把本来的代码正文失落
//setlocale(LC_ALL,"");
//bindtextdomain(PACKAGE,LOCALEDIR);
//textdomain(PACKAGE);
看了源代码以后,你是否是发明实在Linux下令实在其实不太奥秘。
注:本文附件中的tailf.c已修正成上面的模样。
C代码http://codingstandards.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf
- /*tailf.c--tailalogfileandthenfollowit*Created:TueJan915:49:211996byfaith@acm.org*Copyright1996,2003RickardE.Faith(faith@acm.org)**Permissionisherebygranted,freeofcharge,toanypersonobtaininga*copyofthissoftwareandassociateddocumentationfiles(the"Software"),*todealintheSoftwarewithoutrestriction,includingwithoutlimitation*therightstouse,copy,modify,merge,publish,distribute,sublicense,*and/orsellcopiesoftheSoftware,andtopermitpersonstowhomthe*Softwareisfurnishedtodoso,subjecttothefollowingconditions:**Theabovecopyrightnoticeandthispermissionnoticeshallbeincluded*inallcopiesorsubstantialportionsoftheSoftware.**THESOFTWAREISPROVIDED"ASIS",WITHOUTWARRANTYOFANYKIND,EXPRESSOR*IMPLIED,INCLUDINGBUTNOTLIMITEDTOTHEWARRANTIESOFMERCHANTABILITY,*FITNESSFORAPARTICULARPURPOSEANDNONINFRINGEMENT.INNOEVENTSHALL*THEAUTHORSORCOPYRIGHTHOLDERSBELIABLEFORANYCLAIM,DAMAGESOR*OTHERLIABILITY,WHETHERINANACTIONOFCONTRACT,TORTOROTHERWISE,*ARISINGFROM,OUTOFORINCONNECTIONWITHTHESOFTWAREORTHEUSEOR*OTHERDEALINGSINTHESOFTWARE.**less-Fandtail-fcauseadiskaccesseveryfiveseconds.This*programavoidsthisproblembywaitingforthefilesizetochange.*Hence,thefileisnotaccessed,andtheaccesstimedoesnotneedtobe*flushedbacktodisk.Thisissortofa"stealth"tail.*/#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<malloc.h>#include<sys/stat.h>//#include"nls.h"#define_(s)sstaticsize_tfilesize(constchar*filename){structstatsb;if(!stat(filename,&sb))returnsb.st_size;return0;}staticvoidtailf(constchar*filename,intlines){char**buffer;inthead=0;inttail=0;FILE*str;inti;if(!(str=fopen(filename,"r"))){fprintf(stderr,_("Cannotopen"%s"forread
- "),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);}intmain(intargc,char**argv){charbuffer[BUFSIZ];size_tosize,nsize;FILE*str;constchar*filename;intcount;//setlocale(LC_ALL,"");//bindtextdomain(PACKAGE,LOCALEDIR);//textdomain(PACKAGE);if(argc!=2){fprintf(stderr,_("Usage:tailflogfile
- "));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,_("Cannotopen"%s"forread
- "),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*/}return0;}
复制代码
[root@smsgwtailf]#gcc-Wall-o/usr/bin/tailftailf.c
[root@smsgwtailf]#tailf
Usage:tailflogfile
[root@smsgwtailf]#
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们! |
|