當前位置:編程學習大全網 - 編程語言 - 13、linux上怎麽動態監控壹個文件的內容變化?

13、linux上怎麽動態監控壹個文件的內容變化?

用途說明

tail命令可以輸出文件的尾部內容,默認情況下它顯示文件的最後十行。它常用來動態監視文件的尾部內容的增長情況,比如用來監視日誌文件的變化。與tail命令對應的是head命令,用來顯示文件頭部內容。

常用參數

格式:tail file

輸出指定文件file的尾部內容,默認輸出最後十行內容(outputthe last part of files。Print the last 10 lines of each FILE tostandard output. )

格式:tail file1 file2...

指定多個文件時,會顯示每個文件的文件名稱,再顯示該文件的尾部內容(Withmore than one FILE, precede each with a header giving the file name.)

格式:tail

格式:tail -

不指定文件時,表明從標準輸入讀取內容,這通常用在管道線後面,把前壹個命令的輸出作為tail的輸入內容(Withno FILE, or when FILE is -, read standard input.)

格式:tail -n file

格式:tail -n n file

格式:tail --lines=n

顯示文件最後n 行,比如tail -20 file就是顯示文件最後10行,這個參數可以配合其他參數與使用。註意上面三種格式的斜體n 是實際要顯示的行數的數值。

註意:tail-n可以顯示最後n行的文本內容。那麽有沒有壹種方式顯示從n行開始的文本內容,答案是肯定的。

tail -n +4file表示顯示文件file從第4行開始的內容。從1開始計數。

格式:tail -f file

動態跟蹤文件file的增長情況(outputappended data as the filegrows),tail會每隔壹秒去檢查壹下文件是否增加新的內容,如果增加就追加在原來的輸出後面顯示。但這種情況,必須保證在執行tail命令時,文件已經存在。

如果想終止tail-f的輸出,按Ctrl+C中斷tail程序即可。如果按Ctrl+C不能中斷輸出,那麽可以在別的終端上執行killall tail強行終止。

註意:采用tail-f來監控文件變化情況時,在某些情況會不太靈。比如在Java應用程序中采用log4j日誌時,每隔1個小時生成壹個新的日誌文件,當前的日誌輸出在 LOG4J.LOG中,當壹個小時過去後,log4j會將LOG4J.LOG改名成LOG4J.yyyy-mm-dd-HH的形式。那麽這個時候tail -f就不能動態輸出新的日誌內容了。tail命令本身提供了很多參數,似乎都不能完美的解決這個問題。最後只好編寫了壹個腳本ftail.sh來跟蹤日 誌,詳見《Linux下實時跟蹤log4j日誌文件的bash腳本 - 增強了tail -f的功能 》。剛才我仔細查看了tail的手冊頁,發現tail -F就能夠做到跟蹤這種類型的日誌。轉念壹想,這種需求應該早就被Linux世界的人給滿足了的。

格式:tail -F file

格式:tail--follow=name --retry file

功能與tail -ffile相同,也是動態跟蹤文件的變化,不同的是執行此命令時文件可以不存在。

以上處理都是針對文本文件的,下面是針對二進制文件的情形。

格式:tail -c n file

取文件file的最後n個字節。

格式:tail -c +n file

取文件file的第n個字節後的內容。從1開始計數。

使用示例

示例壹 輸出文件尾部

先使用seq命令輸出20個數字保存到1.txt,然後嘗試使用tail命令。

[root@new55 ~]# seq 20 >1.txt

[root@new55 ~]# cat 1.txt

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

[root@new55 ~]# tail 1.txt

11

12

13

14

15

16

17

18

19

20

[root@new55 ~]# tail -3 1.txt

18

19

20

[root@new55 ~]# tail -n 3 1.txt

18

19

20

[root@new55 ~]# tail --lines=3 1.txt

18

19

20

[root@new55 ~]# tail -n +14 1.txt

14

15

16

17

18

19

20

[root@new55 ~]#

示例二 動態跟蹤tomcat輸出

動態跟蹤tomcat輸出。

[root@web logs]# tail -f catalina.out

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)

at org.apache.coyote.modations=面議,serve_principal=wjw12580,job_summary=大專,1年以上5s管理工作經驗,電腦操作熟練,,}}

2010-12-0313:23:02,302 [es inaccessible later; useful when following by name, i.e., with --follow=name。 tail命令開始執行時文件不存在或者執行過程中文件不能訪問,會不斷重試。

關於--follow的說明:-f, --follow[={name|descriptor}]output appended data as the file grows; -f, --follow, and --follow=descriptorare equivalent 。--follow=descriptor表明跟蹤的是文件描述符, --follow=name表明跟蹤的是文件名稱。 如果文件名稱改掉之後,還想繼續跟蹤原文件名稱對應的尾部內容,就得使用-F選項而不是-f選項了。

[root@webimx_server]# tail -F log/IMX.LOG

14:13:28.892 INFO ImxConnection[6] imx.server.ImxConnection - RXIMX_ACTIVE_TEST{seq=3460,client_id=1291343201649042,presence_status=1(presence_status_online),}

14:13:28.892 DEBUGImxConnection[6] org.logicalcobwebs.proxool.ImxDB - 006417 (01/02/00) -Connection #9 served

14:13:28.892 INFO ImxConnection[6] imx.dbo.ImxOnlineInfoRow - EXEC SQL UPDATEimx_online_info SET last_active_time = '2010-12-03 14:13:28.0' WHERE account ='zhy'

14:13:28.894 DEBUGImxConnection[6] org.logicalcobwebs.proxool.ImxDB - UPDATE imx_online_info SETlast_active_time = '2010-12-03 14:13:28.0' WHERE account = 'zhy'; (1milliseconds)

14:13:28.894 DEBUGImxConnection[6] org.logicalcobwebs.proxool.ImxDB - 006417 (00/02/00) -Connection #9 returned (now AVAILABLE)

14:13:29.625 INFO ImxConnection[6] imx.server.ImxConnection - RXIMX_ACTIVE_TEST{seq=3461,client_id=1291343201649042,presence_status=1(presence_status_online),}

14:13:29.626 DEBUGImxConnection[6] org.logicalcobwebs.proxool.ImxDB - 006418 (01/02/00) -Connection #8 served

14:13:29.626 INFO ImxConnection[6] imx.dbo.ImxOnlineInfoRow - EXEC SQL UPDATEimx_online_info SET last_active_time = '2010-12-03 14:13:29.0' WHERE account ='zhy'

14:13:29.627 DEBUGImxConnection[6] org.logicalcobwebs.proxool.ImxDB - UPDATE imx_online_info SETlast_active_time = '2010-12-03 14:13:29.0' WHERE account = 'zhy'; (0milliseconds)

14:13:29.653 DEBUGImxConnection[6] org.logicalcobwebs.proxool.ImxDB - 006418 (00/02/00) -Connection #8 returned (now AVAILABLE)

Ctrl+C

[root@webimx_server]#

  • 上一篇:雷軍是壹個什麽樣的人?
  • 下一篇:想知道壹些關於徐良的信息?朋友們,幫幫忙哦!!!
  • copyright 2024編程學習大全網