歡迎光臨
每天分享高質量文章

Linux終端裡的記錄器

我們在除錯程式的時候,免不了要去抓一些 log ,然後進行分析。如果 log 量不是很大的話,那很簡單,只需簡單的複製貼上就好。但是如果做一些壓力測試,產生大量 log ,而且系統記憶體又比較小(比如嵌入式裝置),那要怎麼處理呢?

當然,secureCRT 和 mobaXterm 都有將日誌儲存到本地的功能,使用起來也是很方便。但是有些工具,比如 putty,就沒這樣的功能了。

這時終端裡的記錄器—— script 就派上用場了。

使用場景

1、除錯會產生大量 log 的應用程式,並且需要儲存到本地進行進一步分析;

2、與同事協同工作,自己將工作完成了一半,可以將操作過程記錄下來,發給同事,同事可以根據記錄接著工作;

3、讓人遠端協助你,擔心對方使壞,同時也可以留下案底,最好將他的操作記錄下來

如何使用 script 命令?

預設情況下,直接輸入 script 這個命令即可,它會在當前目錄自動建立一個 typescript 檔案,之後你在此終端的所有操作都會被記錄在這個檔案裡。

記錄檔案是一個文字檔案,可以使用任意的文字工具開啟檢視。

如果要退出記錄,可以在終端裡按快捷鍵 ctrl + D 或直接輸入 exit 。在退出 script 前,你會發現,記錄檔案大小為 0 Kb,當退出後,檔案大小會變大。

[alvin@VM_0_16_centos test]$ script
Script started, file is typescript
[alvin@VM_0_16_centos test]$ echo hello
hello
[alvin@VM_0_16_centos test]$ ls
test1.py  test2  test2.cpp  test2.py  test3  test3.c  test.py  typescript  WeixinBot  wxpy  wxRobot
[alvin@VM_0_16_centos test]$ exit
exit
Script done, file is typescript

如果我們想要自己起個檔案名,或者將檔案放在其它位置,那麼我們可以直接在 script 後面跟上檔案名即可。

[alvin@VM_0_16_centos test]$ script ~/alvin-script
Script started, file is /home/alvin/alvin-script
[alvin@VM_0_16_centos test]$ ll
total 64
-rw-rw-r--  1 alvin alvin    21 Nov 10 09:40 test1.py
-rwxrwxr-x  1 alvin alvin 14074 Dec 31 07:35 test2
-rw-rw-r--  1 alvin alvin   403 Dec 31 07:35 test2.cpp
-rw-rw-r--  1 alvin alvin  2093 Nov 10 10:50 test2.py
-rwxrwxr-x  1 alvin alvin  8553 Jan  7 20:03 test3
-rw-rw-r--  1 alvin alvin    78 Jan  7 20:03 test3.c
-rw-rw-r--  1 alvin alvin    94 Nov  9 23:25 test.py
-rw-rw-r--  1 alvin alvin   489 Jan 11 12:07 typescript
drwxrwxr-x  6 alvin alvin  4096 Nov 10 11:19 WeixinBot
drwxrwxr-x  6 alvin alvin  4096 Nov 10 11:30 wxpy
drwxrwxr-x 11 alvin alvin  4096 Nov 10 11:34 wxRobot
[alvin@VM_0_16_centos test]$ echo hello
hello
[alvin@VM_0_16_centos test]$ exit
exit
Script done, file is /home/alvin/alvin-script

學會這兩個基本操作,可以應付很多場景下需要記錄終端的場景。

如何使用 script 與同事協作?

現在有一項工作,需要與同事一起協作,我完成一半,他完成另一半。

首先,我來做我的工作,用 script 記錄一下我的工作過程:

[alvin@VM_0_16_centos test]$ script cooperate-job
Script started, file is cooperate-job
[alvin@VM_0_16_centos test]$ echo this is alvin_s job
this is alvin_s job
[alvin@VM_0_16_centos test]$ ls
cooperate-job  test1.py  test2  test2.cpp  test2.py  test3  test3.c  test.py  typescript  WeixinBot  wxpy  wxRobot
[alvin@VM_0_16_centos test]$ exit
exit
Script done, file is cooperate-job

工作完成之後,將記錄檔案發給同事,他可以使用文字工具開啟,就可以知道你的進度了,然後接著你的進度幹活。

如果他要接著在你的記錄檔案裡記錄他的操作的話,可以加一個 -a 選項,即 append 的縮寫。

[alvin@VM_0_16_centos test]$ script -a cooperate-job
Script started, file is cooperate-job
[alvin@VM_0_16_centos test]$ echo this is harry_s job
this is harry_s job
[alvin@VM_0_16_centos test]$ pwd
/home/alvin/test
[alvin@VM_0_16_centos test]$ exit
exit
Script done, file is cooperate-job

請他人遠端協助時,如何記錄他的操作過程?

讓他人登陸到自己的電腦,如果是熟人還好,是陌生人的話心裡多少會有些不踏實。為了放心一下,我們還是偷偷記錄一下他的所作所為吧。

我們可以將 script 命令新增到 Shell 配置檔案中,使用者一旦登入進來,script 命令就自動啟動,並記錄操作者的所有操作過程。

實現這個目的,我們可以修改 .bash_profile 檔案。

vim ~/.bash_profile

在最後一行,我們將 script 命令新增進去:

/usr/bin/script -qa your_path #補齊自己的路徑

然後儲存,使用 source 或 . 命令使它生效。下次其它人登入到系統時,script 就會自動執行,並將記錄檔案儲存在你所指定的位置。

在這裡,-q 選項代表靜默記錄,對方將不知道你在後臺記錄。如果不使用這個選項,則他會收到這個提示:

Last login: Fri Jan 11 15:13:37 2019 from 119.33.28.6
Script started, file is /home/alvin/test/script-file  #提示
[alvin@VM_0_16_centos ~]$

    贊(0)

    分享創造快樂