Saturday, January 19, 2013

如何紀錄linux終端的操作日誌


linux終端下,為方便檢查操作中可能出現的錯誤,以及避免螢幕滾屏的限制,我們可以把操作日誌記錄下來。常用的工具有 screen,script,以及tee等。
  1. screen — screen manager with VT100/ANSI terminal emulatio
  > screen -L
  這裡是我們的操作
  > exit
  結束後會生成一個類似這樣的檔案名: screenlog.0
  > more screenlog.0
  這樣可以查看你剛才進行的操作,發現可能存在的問題
  2. script  — make typescript of terminal session
  > script
  我們的操作
  > exit
  生成一個這樣的檔:typescript
  > more typescript
  這裡查看我們進行的操作
  3. tee –  read from standard input and write to standard output and files
  這個命令可以讀取終端輸入輸出到終端或者檔中,有時候可以用來記錄make等命令可能產生的輸出到終端的大量內容輸出到檔中去。這樣可以方便記 錄這些命令的日誌。
  > make 2>&1 | tee make.log
  當然,我們也可以直接重定向到一個檔中
  > make > make.log
  PS: 2>&1是為了記錄錯誤日誌
  if you want to filter the control symbols, try to use the "col" command like this:
  $ cat screenlog.0 | col -b > screenlog
  or
  $ cat typescript | col -b > scriptlog
  還有一個比較好用的命令tail -f
  比如把輸出定向到text
  ./run.sh>text
  tail -f  text 這個命令會follow輸出定向檔的內容


No comments:

Post a Comment