Linux终端里的记录器

系统 Linux
我们在调试程序的时候,免不了要去抓一些 log ,然后进行分析。如果做一些压力测试,产生大量 log ,而且系统内存又比较小(比如嵌入式设备),那要怎么处理呢?这时终端里的记录器—— script 就派上用场了。

我们在调试程序的时候,免不了要去抓一些 log ,然后进行分析。如果 log 量不是很大的话,那很简单,只需简单的复制粘贴就好。但是如果做一些压力测试,产生大量 log ,而且系统内存又比较小(比如嵌入式设备),那要怎么处理呢?

当然,secureCRT 和 mobaXterm 都有将日志保存到本地的功能,使用起来也是很方便。但是有些工具,比如 putty,就没这样的功能了。

这时终端里的记录器—— script 就派上用场了。

使用场景

  • 调试会产生大量 log 的应用程序,并且需要保存到本地进行进一步分析;
  • 与同事协同工作,自己将工作完成了一半,可以将操作过程记录下来,发给同事,同事可以根据记录接着工作;
  • 让人远程协助你,担心对方使坏,同时也可以留下案底,最好将他的操作记录下来

如何使用 script 命令?

默认情况下,直接输入 script 这个命令即可,它会在当前目录自动创建一个 typescript 文件,之后你在此终端的所有操作都会被记录在这个文件里。

记录文件是一个文本文件,可以使用任意的文本工具打开查看。

如果要退出记录,可以在终端里按快捷键 ctrl + D 或直接输入 exit 。在退出 script 前,你会发现,记录文件大小为 0 Kb,当退出后,文件大小会变大。

  1. [alvin@VM_0_16_centos test]$ script 
  2. Script started, file is typescript 
  3. [alvin@VM_0_16_centos test]$ echo hello 
  4. hello 
  5. [alvin@VM_0_16_centos test]$ ls 
  6. test1.py  test2  test2.cpp  test2.py  test3  test3.c  test.py  typescript  WeixinBot  wxpy  wxRobot 
  7. [alvin@VM_0_16_centos test]$ exit 
  8. exit 
  9. Script done, file is typescript 

如果我们想要自己起个文件名,或者将文件放在其它位置,那么我们可以直接在 script 后面跟上文件名即可。

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

学会这两个基本操作,可以应付很多场景下需要记录终端的场景。

如何使用 script 与同事协作?

现在有一项工作,需要与同事一起协作,我完成一半,他完成另一半。

首先,我来做我的工作,用 script 记录一下我的工作过程:

  1. [alvin@VM_0_16_centos test]$ script cooperate-job 
  2. Script started, file is cooperate-job 
  3. [alvin@VM_0_16_centos test]$ echo this is alvin_s job 
  4. this is alvin_s job 
  5. [alvin@VM_0_16_centos test]$ ls 
  6. cooperate-job  test1.py  test2  test2.cpp  test2.py  test3  test3.c  test.py  typescript  WeixinBot  wxpy  wxRobot 
  7. [alvin@VM_0_16_centos test]$ exit 
  8. exit 
  9. Script done, file is cooperate-job 

工作完成之后,将记录文件发给同事,他可以使用文本工具打开,就可以知道你的进度了,然后接着你的进度干活。

如果他要接着在你的记录文件里记录他的操作的话,可以加一个 -a 选项,即 append 的缩写。

  1. [alvin@VM_0_16_centos test]$ script -a cooperate-job 
  2. Script started, file is cooperate-job 
  3. [alvin@VM_0_16_centos test]$ echo this is harry_s job 
  4. this is harry_s job 
  5. [alvin@VM_0_16_centos test]$ pwd 
  6. /home/alvin/test 
  7. [alvin@VM_0_16_centos test]$ exit 
  8. exit 
  9. Script done, file is cooperate-job 

请他人远程协助时,如何记录他的操作过程?

让他人登陆到自己的电脑,如果是熟人还好,是陌生人的话心里多少会有些不踏实。为了放心一下,我们还是偷偷记录一下他的所作所为吧。

我们可以将 script 命令添加到 Shell 配置文件中,用户一旦登录进来,script 命令就自动启动,并记录操作者的所有操作过程。

实现这个目的,我们可以修改 .bash_profile 文件。

  1. vim ~/.bash_profile 

在最后一行,我们将 script 命令添加进去:

  1. /usr/bin/script -qa your_path #补齐自己的路径 

然后保存,使用 source 或 . 命令使它生效。下次其它人登录到系统时,script 就会自动运行,并将记录文件保存在你所指定的位置。

在这里,-q 选项代表静默记录,对方将不知道你在后台记录。如果不使用这个选项,则他会收到这个提示:

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

本文授权转载自公众号「良许Linux」。良许,世界500强外企Linux开发工程师,公众号里分享大量Linux干货,欢迎关注!

 

责任编辑:赵宁宁 来源: 今日头条
相关推荐

2014-06-16 09:44:56

Linux scripLinux命令

2023-03-02 16:19:54

2021-12-01 07:02:55

Python 记录器按键

2013-10-18 17:42:59

2022-06-10 11:51:11

微软Word文档漏洞

2012-05-25 10:40:58

Win7操作系统

2017-12-13 16:10:27

惠普电脑键盘

2022-04-16 19:04:49

Linuxpass 命令

2015-05-11 11:10:42

2018-01-31 08:57:00

2011-04-21 15:47:19

笔记本记录器

2023-06-29 11:31:35

2016-11-09 09:48:03

Arch Linux设定WiFi网络

2011-10-28 13:26:09

Win7

2013-11-01 17:36:35

2022-01-27 15:37:34

Asciinema终端会话记录器开源

2019-10-08 14:02:18

Linux命令终端会话

2021-08-17 13:54:31

数字化

2017-11-27 09:31:40

Linux终端模拟器Xiki

2017-04-19 18:38:43

Linux终端仿真器
点赞
收藏

51CTO技术栈公众号