关于linux:Linux终端里的记录器

8次阅读

共计 3002 个字符,预计需要花费 8 分钟才能阅读完成。

咱们在调试程序的时候,免不了要去抓一些 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 ~]$

最初,最近很多小伙伴找我要 Linux 学习路线图 ,于是我依据本人的教训,利用业余时间熬夜肝了一个月,整顿了一份电子书。无论你是面试还是自我晋升,置信都会对你有帮忙!

收费送给大家,只求大家金指给我点个赞!

电子书 | Linux 开发学习路线图

也心愿有小伙伴能退出我,把这份电子书做得更完满!

有播种?心愿老铁们来个三连击,给更多的人看到这篇文章

举荐浏览:

  • 干货 | 程序员进阶架构师必备资源免费送
  • 神器 | 反对搜寻的资源网站
正文完
 0