1. 程式人生 > 其它 >【基礎】tail命令檢視日誌

【基礎】tail命令檢視日誌

一、tail 命令介紹

tail 命令可以將檔案指定位置到檔案結束的內容寫到標準輸出。

如果你不知道tail命令怎樣使用,可以在命令列執行命令tail --help就能看到tail命令介紹和詳細的引數使用介紹,內容如下(我幫大家翻譯了一下)。

[root@yanggongzi ~]# tail --help
Usage: tail [OPTION]... [FILE]...
Print the last 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.
將每個檔案的最後10行列印到標準輸出。
如果有多個檔案,在每個檔案之前都有一個給出檔名的標頭檔案。
沒有檔案,或者當檔案為-時,讀取標準輸入。

Mandatory arguments to long options are mandatory for short options too.
長選項必須用的引數在使用短選項時也是必須的。
  -c, --bytes=K            output the last K bytes;
                           or use -c +K to output bytes starting with the Kth of each file
                           輸出最後的 K 個位元組;
			               或者使用 -c +K 從每個檔案的第K位元組開始列印。
  -f, --follow[={name|descriptor}]
                           output appended data as the file grows;
                           an absent option argument means 'descriptor'
                           隨著檔案的增長,輸出附加資料;(動態輸出最新的資訊);
                           沒有選項引數意味著“描述符”
                           
  -F                       same as --follow=name --retry
                           與 --follow=name --retry 作用相同
                           
  -n, --lines=K            output the last K lines, instead of the last 10;
                           or use -n +K to output starting with the Kth
                           輸出最後的K行,而不是最後的10行;
                           或者使用-n +K從第K個開始輸出
                           
      --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
                           使用——follow=name,在N次(預設為5次)迭代後,重新開啟一個大小沒有改變的檔案,看看它是否被解除連結或重新命名(這是旋轉日誌檔案的常見情況);
                           對於inotify,這個選項很少有用
                             
      --pid=PID            with -f, terminate after process ID, PID dies
                           與“-f”選項連用,當指定的程序號的程序終止後,自動退出tail命令

  -q, --quiet, --silent    never output headers giving file names
                           當有多個檔案引數時,不輸出各個檔名
                           
      --retry              keep trying to open a file if it is inaccessible
                           即是在tail命令啟動時,檔案不可訪問或者檔案稍後變得不可訪問,都始終嘗試開啟檔案。使用此選項時需要與選項“——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
                           與“-f”選項連用,指定監視檔案變化時間隔的秒數(預設為1.0);
			               使用inotify和-pid=P,每N秒檢查程序P至少一次
			               
  -v, --verbose            always output headers giving file names
                           當有多個檔案引數時,總是輸出各個檔名
                           
      --help               display this help and exit
                           顯示此幫助資訊並退出
                           
      --version            output version information and exit
                           顯示版本資訊並退出

If the first character of K (the number of bytes or lines) is a '+',
print beginning with the Kth item from the start of each file, otherwise,
print the last K items in the file.  K may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.
如果K前面的字元(位元組數或行數)是'+',每個檔案從第K項開始列印,否則,列印檔案中最後的K項。K可能有一個乘數字尾:b 512,kB 1000,K 1024,MB 1000 1000,M 1024 1024,GB 1000 1000,G 1024 1024 1024,等等,對於T,P,E,Z,y。

With --follow (-f), tail defaults to following the file descriptor, which
means that even if a tail"ed file is renamed, tail will continue to track
its end.  This default behavior is not desirable when you really want to
track the actual name of the file, not the file descriptor (e.g., log
rotation).  Use --follow=name in that case.  That causes tail to track the
named file in a way that accommodates renaming, removal and creation.
使用——follow (-f), tail預設跟隨檔案描述符,這意味著即使重新命名了尾部檔案,tail也將繼續跟蹤其尾部。
當您真正想要跟蹤檔案的實際名稱而不是檔案描述符(例如,日誌旋轉)時,這種預設行為是不可取的。
在這種情況下使用——follow=name。這將導致tail以一種適合重新命名、刪除和建立的方式跟蹤已命名檔案。

二、tail 命令使用示例

1、輸出最後200個字元

tail -c 200 test.log

2、從第900個字元開始輸出,一直到最後

tail -c +900 test.log

3、輸出最後20行

tail -n 20 test.log

4、從第36行開始輸出,一直到最後

tail -n +36 test.log

5、輸出指定檔案的最後十行,同時繼續監視檔案內容有無變化,新增內容會繼續輸出,直到按下 [Ctrl-C] 組合鍵退出

tail -f test.log

6、指定多個檔案並輸出檔名

tail -v test1.log test2.log

7、指定多個檔案不輸出檔名

tail -q test1.log test2.log

三、tailf、tail -f、tail -F 的區別

  • tail -f
    等同於–follow=descriptor,根據檔案描述符進行追蹤,當檔案改名或被刪除,追蹤停止

  • tail -F
    等同於–follow=name --retry,根據檔名進行追蹤,並保持重試,即該檔案被刪除或改名後,如果再次建立相同的檔名,會繼續追蹤

  • tailf
    等同於tail -f -n 10(貌似tail -f或-F預設也是列印最後10行,然後追蹤檔案),與tail -f不同的是,如果檔案不增長,它不會去訪問磁碟檔案,所以tailf特別適合那些便攜機上跟蹤日誌檔案,因為它減少了磁碟訪問,可以省電

當我們設定了滾動日誌時,需要持續實時監控最新的日誌輸出,那麼就要用tail -F,而不能用tailf 和 tail -f。因為當日志xxx.log達到了設定閾值重新命名成了xxx01.log時,後兩個命令追蹤的還是xxx01.log檔案而不是新建立的xxx.log檔案,這時就不能繼續監控最新日誌了。

四、常用快捷鍵

【Ctrl】+【S】 暫停重新整理。
【Ctrl】+【Q】繼續重新整理。
【Ctrl】+【C】退出 tail 命令。