Linux监测日志调试利器tail命令详细使用

如果在Linux下调试程序的朋友应该都知道tail命令,它确实是调试程序监测日志文件的能手。打开Linux输入以下命令看看命令的使用帮助

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[root@lee ~] # tail --help
用法: tail [选项]... [文件]...
显示每个指定文件的最后10 行到标准输出。
若指定了多于一个文件,程序会在每段输出的开始添加相应文件名作为头。
如果不指定文件或文件为 "-" ,则从标准输入读取数据。
 
长选项必须使用的参数对于短选项时也是必需使用的。
   -c, --bytes=K         输出最后K 字节;另外,使用-c +K 从每个文件的
                         第K 字节输出
   -f, --follow[={name|descriptor}]
                 即时输出文件变化后追加的数据。
                         -f, --follow 等于--follow=descriptor
   -F            即--follow=name --retry
   -n, --lines=K            output the last K lines, instead of the last 10;
                            or use -n +K to output lines starting with the Kth
       --max-unchanged-stats=N
                            with --follow=name, reopen a FILE which has not
                            changed size after N (default 5) iterations
                            to see if it has been unlinked or renamed
                            (this is the usual case of rotated log files).
                            With inotify, this option is rarely useful.
       --pid=PID         同 -f 一起使用,当 PID 所对应的进程死去后终止
   -q, --quiet, --silent 不输出给出文件名的头
       --retry           即使目标文件不可访问依然试图打开;在与参数
                         --follow=name 同时使用时常常有用。
   -s, -- sleep -interval=N   with -f, sleep for approximately N seconds
                              (default 1.0) between iterations.
                            With inotify and --pid=P, check process P at
                            least once every N seconds.
   - v , --verbose            always output headers giving file names
       --help            显示此帮助信息并退出
       --version         显示版本信息并退出
 
如果字节数或行数K 的第一个字符是 "+" ,输出从文件开始第K 个项目,否则输出文件
最后K 个项目。K 可以使用一下几种单位之一:
b 512,kB 1000,K 1024,MB 1000*1000,M 1024*1024,
GB 1000*1000*1000,G 1024*1024*1024,以及T,P,E,Z,Y。
 
如果您希望即时追查一个文件的有效名称而非描述内容(例如循环日志),默认
的程序动作并不如您所愿。在这种场合可以使用--follow=name 选项,它会使
tail 定期追踪打开给定名称的文件,以确认它是否被删除或被其它某些程序重新创建过。
 
请向bug-coreutils@gnu.org 报告 tail 的错误
GNU coreutils 项目主页:<http: //www .gnu.org /software/coreutils/ >
GNU 软件一般性帮助:<http: //www .gnu.org /gethelp/ >
请向<http: //translationproject .org /team/zh_CN .html> 报告 tail 的翻译错误
要获取完整文档,请运行:info coreutils 'tail invocation'

以上是在CentOS6.4中文版下执行结果,中文翻译有点别扭。

假如有一个文件test.txt,内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@lee ~] # cat test.txt
这是第1行
这是第2行
这是第3行
这是第4行
这是第5行
这是第6行
这是第7行
这是第8行
这是第9行
这是第10行
这是第11行
这是第12行
这是第13行
这是第14行
这是第15行
这是第16行
这是第17行
这是第18行
这是第19行
这是第20行
这是第21行
这是第22行
这是第23行
这是第24行
[root@lee ~] #

我们使用tail命令查看test.txt内容

1
2
3
4
5
6
7
8
9
10
11
12
[root@lee ~] # tail test.txt
这是第15行
这是第16行
这是第17行
这是第18行
这是第19行
这是第20行
这是第21行
这是第22行
这是第23行
这是第24行
[root@lee ~] #

默认只读取文件末尾10行数据,相当于

1
2
3
4
5
6
7
8
9
10
11
[root@lee ~] # tail -n 10 test.txt
这是第15行
这是第16行
这是第17行
这是第18行
这是第19行
这是第20行
这是第21行
这是第22行
这是第23行
这是第24行

当然,您也可以修改参数,读取你想要的行数,比如读取15行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@lee ~] # tail -n 15 test.txt 
这是第10行
这是第11行
这是第12行
这是第13行
这是第14行
这是第15行
这是第16行
这是第17行
这是第18行
这是第19行
这是第20行
这是第21行
这是第22行
这是第23行
这是第24行
[root@lee ~] #

参数-c 字节输出,遇到中文字符有可能被截断,我们来看看例子

例子1,中文字符被截断

1
2
[root@lee ~] # tail -c 3 test.txt  
]0;root@lee:~[root@lee ~] #

例子2,完整的截取

1
2
3
[root@lee ~] # tail -c 8 test.txt
[root@lee ~] #

如果在数字参数前补个“+”号即表示从文档开始截取

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@lee ~] # tail -c +0 test.txt  
这是第1行
这是第2行
这是第3行
这是第4行
这是第5行
这是第6行
这是第7行
这是第8行
这是第9行
这是第10行
这是第11行
这是第12行
这是第13行
这是第14行
这是第15行
这是第16行
这是第17行
这是第18行
这是第19行
这是第20行
这是第21行
这是第22行
这是第23行
这是第24行
[root@lee ~] # tail -c +300 test.txt 
这是第22行
这是第23行
这是第24行
[root@lee ~] #

-f 与 -F参数的使用

f/F参数是tail命令强大之处,它可以实时把最新的内容给打印出来。来做个例子

使用-f命令监测test.txt文件

1
2
3
4
5
6
7
8
9
10
11
[root@lee ~] # tail -f test.txt
这是第15行
这是第16行
这是第17行
这是第18行
这是第19行
这是第20行
这是第21行
这是第22行
这是第23行
这是第24行

使用另外一个ssh客户端给test.txt末尾添加新内容

1
[root@lee ~] # echo '这是最新添加的内容' >> test.txt

监测实时把刚才添加的内容给打印出来

1
2
3
4
5
6
7
8
9
10
11
12
[root@lee ~] # tail -f test.txt
这是第15行
这是第16行
这是第17行
这是第18行
这是第19行
这是第20行
这是第21行
这是第22行
这是第23行
这是第24行
这是最新添加的内容

CTRL + C 终止监测。-F与-f的区别在于-f指定文件不存在会返回错误,-F指定文件不存在同样也可以监测,比如看看下面的例子

1
2
3
4
5
[root@lee ~] # find ./ -name abc.txt
[root@lee ~] # tail -f abc.txt
tail : 无法打开 "abc.txt" 读取数据: 没有那个文件或目录
[root@lee ~] # tail -F abc.txt
tail : 无法打开 "abc.txt" 读取数据: 没有那个文件或目录

虽然提示文件不存在,但还是进入了监测状态,我们在另一个ssh客户端创建一个abc.txt文件并向里边输入内容

1
[root@lee ~] # echo '这是abc.txt的内容' > abc.txt

这边监测abc.txt的ssh客户端即时显示刚创建的abc.txt内容

1
2
3
4
[root@lee ~] # tail -F abc.txt
tail : 无法打开 "abc.txt" 读取数据: 没有那个文件或目录
tail : "abc.txt" 已经可以访问
这是abc.txt的内容

可以配合-n指定行数

1
2
3
4
5
6
[root@lee ~] # tail -f -n 5 test.txt
这是第21行
这是第22行
这是第23行
这是第24行
这是最新添加的内容

如果在-n 的参数前加"+"号,表示从文档的第几行开始读取,如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@lee ~] # tail -f -n +5 test.txt
这是第5行
这是第6行
这是第7行
这是第8行
这是第9行
这是第10行
这是第11行
这是第12行
这是第13行
这是第14行
这是第15行
这是第16行
这是第17行
这是第18行
这是第19行
这是第20行
这是第21行
这是第22行
这是第23行
这是第24行
这是最新添加的内容

平常我们可以利用此命令监测站点访问日志,程序日志打印等等。

比如监测本站access记录

1
2
3
4
5
6
7
8
9
10
11
[root@qttc logs] # tail -f access_qttc.log
42.121.30.35 - - [18 /Apr/2013 :13:21:44 +0800] "GET /error.html HTTP/1.1" 200 9951 "-" "PHP/5.4.10" -
103.29.134.203 - - [18 /Apr/2013 :13:21:44 +0800] "GET /tag/%D1%C5%BB%A2 HTTP/1.0" 302 9758 "-" "Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/17.0 Firefox/17.0" -
103.29.134.203 - - [18 /Apr/2013 :13:21:44 +0800] "GET /error.html HTTP/1.0" 200 9938 "-" "Mozilla/5.0 (Windows NT 6.1; rv:17.0) Gecko/17.0 Firefox/17.0" -
66.249.74.179 - - [18 /Apr/2013 :13:22:10 +0800] "GET /upload/2012/13488869742622.png HTTP/1.1" 304 0 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" -
42.121.30.35 - - [18 /Apr/2013 :13:22:36 +0800] "GET /tag/fsockopen HTTP/1.1" 200 10768 "-" "PHP/5.4.10" -
95.211.139.1 - - [18 /Apr/2013 :13:22:36 +0800] "GET /tag/fsockopen HTTP/1.0" 200 10780 "-" "Mozilla/5.0 (compatible; AcoonBot/4.11.1; +http://www.acoon.de/robot.asp)" -
42.121.30.35 - - [18 /Apr/2013 :13:23:38 +0800] "GET /201209210.html HTTP/1.1" 200 22132 "-" "PHP/5.4.10" -
66.249.74.180 - - [18 /Apr/2013 :13:23:38 +0800] "GET /201209210.html HTTP/1.1" 200 6576 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" -
42.121.30.35 - - [18 /Apr/2013 :13:23:46 +0800] "GET /20120555.html HTTP/1.1" 200 40050 "-" "PHP/5.4.10" -
66.249.74.178 - - [18 /Apr/2013 :13:23:46 +0800] "GET /20120555.html HTTP/1.1" 200 8421 "-" "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8B117 Safari/6531.22.7 (compatible; Googlebot-Mobile/2.1; +http://www.google.com/bot.html)" -

当有人访问时会把最新的记录自动打印出来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值