咱们在调试程序的时候,免不了要去抓一些 log ,而后进行分析。若是 log 量不是很大的话,那很简单,只需简单的复制粘贴就好。可是若是作一些压力测试,产生大量 log ,并且系统内存又比较小(好比嵌入式设备),那要怎么处理呢?固然,secureCRT 和 mobaXterm 都有将日志保存到本地的功能,使用起来也是很方便。可是有些工具,好比 putty,就没这样的功能了。这时终端里的记录器—— script 就派上用场了。 |
一、调试会产生大量 log 的应用程序,而且须要保存到本地进行进一步分析;linux
二、与同事协同工做,本身将工做完成了一半,能够将操做过程记录下来,发给同事,同事能够根据记录接着工做;typescript
三、让人远程协助你,担忧对方使坏,同时也能够留下案底,最好将他的操做记录下来vim
默认状况下,直接输入 script 这个命令便可,它会在当前目录自动建立一个 typescript
文件,以后你在此终端的全部操做都会被记录在这个文件里。centos
记录文件是一个文本文件,可使用任意的文本工具打开查看。bash
若是要退出记录,能够在终端里按快捷键 ctrl + D
或直接输入 exit
。在退出 script 前,你会发现,记录文件大小为 0 Kb,当退出后,文件大小会变大。app
[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
学会这两个基本操做,能够应付不少场景下须要记录终端的场景。oop
如今有一项工做,须要与同事一块儿协做,我完成一半,他完成另外一半。测试
首先,我来作个人工做,用 script 记录一下个人工做过程:this
[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 ~]$