良许Linux教程网 干货合集 Linux终端记录神器:script

Linux终端记录神器:script

们在调试程序的时候,免不了要去抓一些 log ,然后进行分析。如果 log 量不是很大的话,那很简单,只需简单的复制粘贴就好。但是如果做一些压力测试,产生大量 log ,而且系统内存又比较小(比如嵌入式设备),那要怎么处理呢?当然,secureCRT 和 mobaXterm 都有将日志保存到本地的功能,使用起来也是很方便。但是有些工具,比如 putty,就没这样的功能了。这时终端里的记录器—— script 就派上用场了。

u=1207255522,3569142432&fm=26&gp=0

使用场景

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 ~]$

以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

img
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部