tail和less这两个命令大家肯定是非常熟悉,但是你是否遇到突然跟踪着跟踪着,文件句柄没了,文件不动了,懵逼了吧,少年。原因你应该也知道,文件被rotate了。那要如何处理呢man tail告诉你答案。最后强烈推荐用less命令。
tail -f filename
tailf filename
-F same as –follow=name –retry
命令
tail -F filename
tail -f --retry filename
另外还有一个神器的命令less file,进去里面按F,变成tailf,ctrl+c中断,变成less。是不是很舒服。
1.tail 测试效果
cp /etc/passwd /data/tmp/
for i in `seq 1 100`;do sleep 1; echo $i >> passwd; done
测试过程
mv -f /data/tmp/passwd /tmp/
方式1:
[root@localhost tmp]# tailf passwd
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
memcached:x:498:499:Memcached daemon:/var/run/memcached:/sbin/nologin
named:x:25:25:Named:/var/named:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
mysql:x:88:88::/home/mysql:/sbin/nologin
ossec:x:502:502::/var/ossec:/sbin/nologin
nagios:x:500:500::/home/nagios:/bin/false
www:x:80:80::/home/www:/sbin/nologin
zabbix:x:501:501::/home/zabbix:/sbin/nologin
mcsvr:x:698:698::/home/mcsvr:/bin/bash
1
2
3
4
5
6
7
结果:中断退出
方式2:
[root@localhost tmp]# tail -f passwd
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
^C
结果:再也没有结果输出
方式3:
[root@localhost tmp]# tail -F passwd
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
tail: `passwd' has become inaccessible: No such file or directory
46
47
48
49
结果:还能继续读到文件
2.tail同时监控多个文件
tail -F /var/log/messages /var/log/secure
3.multitail命令监控多个文件输出
# multitail /var/log/messages /var/log/secure
监控文件和命令
multitail -e "Accepted" /var/log/secure -l "ping baidu.com"
4. 牛逼的less命令(完全可替换tail)
通常我们使用tail -f 命令来实时追踪一个日志文件,很好用,但是有一个问题。例如我发现日志中有一条我关注的信息,但是很快就滚动过去了,这时我必须按 Ctrl+C 结束 tail 命令,然后使用vi或其他工具打开日志文件搜索刚才看到的信息。
tailf的姐妹用法 F命令
F Scroll forward, and keep trying to read when the end of file is reached. Normally this
command would be used when already at the end of the file. It is a way to monitor the tail of a file which is growing while it is being viewed. (The behavior is similar to the "tail -f" command.)
less /var/log/messages 按下F,则进入tailf的模式 ctrl+c,则进入less模式,可以上下翻动