Thursday, August 14, 2008

Process執行中止時便停止追蹤(tail -f)

tail -f 可以持續顯示新增的檔案內容,對於協助追蹤某些檔案的內容變化非常有幫助,但事實上如果process已經沒有繼續執行的時候,那麼意義可能不大,所幸還是有解決的方法,利用--pid= 就可以做到,以下是實際演練:

1. ping hinet苦命的DNS 168.95.1.1,並將結果redirect到/tmp/test
# ping 168.95.1.1 > /tmp/test &

2.利用tail持續追蹤/tmp/test,並輸入ping process的pid
# tail -f --pid=xxxx /tmp/test

3. 中止ping
# killall ping

此時應該會看到類似這樣的行為:
[root@server2 tmp]# ping 168.95.1.1 > /tmp/test &
[1] 11024
[root@server2 tmp]# tail -f --pid=11024 /tmp/test
64 bytes from 168.95.1.1: icmp_seq=1 ttl=239 time=52.6 ms
64 bytes from 168.95.1.1: icmp_seq=2 ttl=246 time=21.4 ms
64 bytes from 168.95.1.1: icmp_seq=3 ttl=246 time=19.9 ms
64 bytes from 168.95.1.1: icmp_seq=4 ttl=246 time=19.6 ms
64 bytes from 168.95.1.1: icmp_seq=5 ttl=246 time=18.4 ms
64 bytes from 168.95.1.1: icmp_seq=6 ttl=239 time=50.9 ms
64 bytes from 168.95.1.1: icmp_seq=7 ttl=246 time=17.6 ms
64 bytes from 168.95.1.1: icmp_seq=8 ttl=246 time=19.1 ms
64 bytes from 168.95.1.1: icmp_seq=9 ttl=239 time=51.4 ms
64 bytes from 168.95.1.1: icmp_seq=10 ttl=246 time=18.1 ms
64 bytes from 168.95.1.1: icmp_seq=11 ttl=239 time=52.3 ms
64 bytes from 168.95.1.1: icmp_seq=12 ttl=246 time=20.6 ms
64 bytes from 168.95.1.1: icmp_seq=13 ttl=246 time=18.9 ms
64 bytes from 168.95.1.1: icmp_seq=14 ttl=239 time=53.4 ms
64 bytes from 168.95.1.1: icmp_seq=15 ttl=246 time=18.6 ms
64 bytes from 168.95.1.1: icmp_seq=16 ttl=246 time=18.5 ms
64 bytes from 168.95.1.1: icmp_seq=17 ttl=246 time=18.9 ms
[1]+ Terminated ping 168.95.1.1 >/tmp/test ping被kill了所以也就停止追蹤了。

No comments: