用途說明
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
"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.095 DEBUG 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]#
示例二 編譯安裝tailf命令
有些Linux版本不帶tailf命令的。
[root@smsgw root]# tailf
-bash: tailf: command not found
[root@smsgw root]#
到代碼搜索網站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
Archive: tailf.zip
inflating: tailf.c
[root@smsgw tailf]# ls
tailf.c tailf.zip
[root@smsgw tailf]# gcc -o /usr/bin/tailf tailf.c
tailf.c:34:17: nls.h: 沒有那個文件或目錄
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]#
修改一下tailf.c的源代碼。
第34行附近:注釋掉頭文件,增加宏定義
//#include "nls.h"
#define _(s) s
第89行附近:把原來的代碼注釋掉
//setlocale(LC_ALL, "");
//bindtextdomain(PACKAGE, LOCALEDIR);
//textdomain(PACKAGE);
看了源代碼之后,你是不是發現其實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
#include
#include
#include
#include
//#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
while (fgets(buffer[tail], BUFSIZ, str)) {
if (++tail >= lines) {
tail = 0;
head = 1;
}
}
if (head) {
for (i = tail; i
for (i = 0; i
} else {
for (i = head; i
}
fflush(stdout);
for (i = 0; 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
[root@smsgw tailf]# tailf
Usage: tailf logfile
[root@smsgw tailf]#
下載次數: 16