文章目录

  • 简单介绍
  • 使用方法详解
    • 常规使用
    • 参数详解
  • 使用场景
  • 总结

作家Philip G. Ezolt在其作品<<Optimizing Linux Performance: A Hands On Guide to Linux Performance Tools>>的1.1章节中建议读者在尝试解决Linux问题的时候,将在终端上跑的命令都完整记录下来,以便后续进行问题复现或者是回查。其中还重点推荐了这个script命令,这一节我们来一起看看这到底是个什么命令。

It is better just to record exactly what you typed. You can then reproduce the exact command line for a future test, and when reviewing past results, you can also see exactly what you measured. The Linux command script (described in detail in Chapter 8, “Utility Tools: Performance Tool Helpers”) or “cut and paste” from a terminal is a good way to do this.

我是T型人小付,一位坚持终身学习的互联网从业者。喜欢我的博客欢迎在csdn上关注我,如果有问题欢迎在底下的评论区交流,谢谢。

简单介绍

script命令用来记录终端的行为及结果,并存储成本地文件,可以直接用编辑器打开进行查看。同时可以记录时间轴信息,利用scriptreplay命令进行动态查看。

使用方法详解

利用script -h查看其帮助文档,如下

[root@testmachine ~]# script -hUsage:script [options] [file]Options:-a, --append            append the output-c, --command <command> run command rather than interactive shell-e, --return            return exit code of the child process-f, --flush             run flush after each write--force             use output file even when it is a link-q, --quiet             be quiet-t, --timing[=<file>]   output timing data to stderr (or to FILE)-V, --version           output version information and exit-h, --help              display this help and exit

常规使用

首先这个命令是可以不接任何参数直接跑的,例如

[root@testmachine ~]# script
Script started, file is typescript
[root@testmachine ~]#

这个时候终端提示我们已经开始记录了,并且记录的内容会存储到当前目录的一个叫做typescript的文件中。我们尝试敲两个命令试试,发现和正常使用终端没有任何异样

[root@testmachine ~]# script
Script started, file is typescript
[root@testmachine ~]# echo "Life is awesome."
Life is awesome.
[root@testmachine ~]# date
Tue Dec  3 17:56:42 +08 2019
[root@testmachine ~]#

打开另一个终端,发现当前目录下确实有个typescript文件,创建时间和跑script的时间吻合,不过大小为0,说明我们的操作并没有被实时记录进去

-rw-r--r--   1 root root    0 Dec  3 17:52 typescript

然后回到之前的终端跑exit命令退出记录,这时候终端提示我们记录结束了

[root@testmachine ~]# script
Script started, file is typescript
[root@testmachine ~]# echo "Life is awesome."
Life is awesome.
[root@testmachine ~]# date
Tue Dec  3 17:56:42 +08 2019
[root@testmachine ~]# exit
exit
Script done, file is typescript
[root@testmachine ~]#

再次查看typescript文件,发现大小变化了,访问时间说明是刚才修改的

-rw-r--r--   1 root root  385 Dec  3 18:00 typescript

打开看看到底记录了些啥

[root@testmachine ~]# cat typescript
Script started on Tue 03 Dec 2019 05:52:57 PM +08
[root@testmachine ~]# echo "Life is awesome."
Life is awesome.
[root@testmachine ~]# date
Tue Dec  3 17:56:42 +08 2019
[root@testmachine ~]# exit
exitScript done on Tue 03 Dec 2019 06:00:55 PM +08
[root@testmachine ~]#

发现记录的开始时间,结束时间,以及中间的所有交互情况都被完整地记录了下来。所以这个命令有个很重要的应用场景就是对远程访问用户进行行为监控,在被监控的用户的~/.profile文件中跑一下这个命令即可,当然我们可以利用参数指定保存的路径。

参数详解

  • file参数
    除了使用默认的文件来存储,还可以指定文件名
[root@testmachine ~]# script script.log
Script started, file is script.log
[root@testmachine ~]#

停止记录后发现效果一样

[root@testmachine ~]# script script.log
Script started, file is script.log
[root@testmachine ~]# date
Tue Dec  3 18:23:33 +08 2019
[root@testmachine ~]# exit
exit
Script done, file is script.log
[root@testmachine ~]#
-rw-r--r--   1 root root  280 Dec  3 18:23 script.log
  • -a参数
    用来在现有的记录文件中进行追加,而不是覆盖,例如我在刚才的记录的基础上再进行一次记录,这一次带上-a参数
[root@testmachine ~]# script -a script.log
Script started, file is script.log
[root@testmachine ~]# echo "You are beautiful."
You are beautiful.
[root@testmachine ~]# date
Tue Dec  3 18:25:43 +08 2019
[root@testmachine ~]# exit
exit
Script done, file is script.log
[root@testmachine ~]#

打开script.log文件会发现新的记录是被追加上去的,而不是覆盖

[root@testmachine ~]# cat script.log
Script started on Tue 03 Dec 2019 06:22:02 PM +08
[root@testmachine ~]# date
Tue Dec  3 18:23:33 +08 2019
[root@testmachine ~]# exit
exitScript done on Tue 03 Dec 2019 06:23:36 PM +08
Script started on Tue 03 Dec 2019 06:25:22 PM +08
[root@testmachine ~]# echo "You are beautiful."
You are beautiful.
[root@testmachine ~]# date
Tue Dec  3 18:25:43 +08 2019
[root@testmachine ~]# exit
exitScript done on Tue 03 Dec 2019 06:25:45 PM +08
[root@testmachine ~]#
  • -f参数
    正常情况下没有停止记录的话,记录的结果是不会实时写入到文件中的。但是加了这个参数就可以强制性让记录实时写入文件。
    例如我开始记录的时候,文件的大小就已经不是0
[root@testmachine ~]# script -f script_force.log
Script started, file is script_force.log
[root@testmachine ~]#
-rw-r--r--   1 root root  103 Dec  3 22:27 script_force.log

随着我不停记录,文件也在不停变大

[root@testmachine ~]# script -f script_force.log
Script started, file is script_force.log
[root@testmachine ~]# date
Tue Dec  3 22:29:24 +08 2019
[root@testmachine ~]#
-rw-r--r--   1 root root  184 Dec  3 22:29 script_force.log

这就引出了script命令另一个应用场景,就是实时远程监控。一个人跑mkfifo foo; script -f foo命令,然后另一个人可以通过跑cat foo去进行实时监控

  • -q参数
    如果不想让终端提示记录的开始和结束可以用这个参数开启安静模式,但是个人觉得这个安静模式容易让人困惑,不太建议使用
[root@testmachine ~]# script -q script_quiet.log
[root@testmachine ~]# date
Tue Dec  3 22:23:01 +08 2019
[root@testmachine ~]# exit
exit
[root@testmachine ~]#
  • -c参数
    后面接一串命令,只是针对这一串命令进行记录,记录完毕就自动退出。
[root@testmachine ~]# script script_command.log -c "date;echo 'I love Linux';uname -a"
Script started, file is script_command.log
Tue Dec  3 22:41:09 +08 2019
I love Linux
Linux testmachine 3.10.0-862.14.4.el7.x86_64 #1 SMP Wed Sep 26 15:12:11 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Script done, file is script_command.log
[root@testmachine ~]#

有的时候我们会发现一个命令在终端上跑没有报错,但是放到cron里面的时候就会出错,就可以在cron里面用script去跑这个命令。解决crontab里面的命令报错是这个命令的第三个应用场景

  • -t参数
    在另一个文件中保存时序信息,方便动态记录。感觉这个就是script命令强大的地方。
[root@testmachine ~]# script -tscript_time.time script_time.log
Script started, file is script_time.log
[root@testmachine ~]# date
Tue Dec  3 22:45:24 +08 2019
[root@testmachine ~]# uname -a
Linux testmachine 3.10.0-862.14.4.el7.x86_64 #1 SMP Wed Sep 26 15:12:11 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@testmachine ~]# exit
exit
Script done, file is script_time.log
[root@testmachine ~]#

上面我用script_time.time文件去存储时序信息,用script_time.log去存放具体的记录内容。注意这里的-tscript_time.time中间没有空格

回放的时候需要用到scriptreplay命令

[root@testmachine ~]# scriptreplay -t script_time.time -s script_time.log

之后会像放电影一样将刚才的操作重复一遍,例如下面的gif我在跑完命令后没有任何操作

使用场景

上面在讲解参数的时候基本将这个命令的使用场景也总结的差不多了

  • 排查问题时候的终端交互记录
  • 远程访问用户的行为记录
  • 远程实时监控
  • 排查crontab的报错问题

如果你能想到别的使用场景欢迎在下面留言分享给我哈

总结

在排查问题的过程当中有效而完整的记录能帮助我们节省不少时间,强大的script命令能帮助我们实现这一功能,赶紧去试一试吧。

强大的Linux终端行为记录和回放工具:script命令详解相关推荐

  1. Linux的scan命令,clamscan-Linux查毒工具的命令详解

    clamscan-Linux查毒工具的命令详解 clamscan命令用于扫描文件和目录,一发现其中包含的计算机病毒,clamscan命令除了扫描linux系统的病毒外,主要扫描的还是文件中包含的win ...

  2. linux中的 ip addr 和 ip link命令详解

    linux中的 ip addr 和 ip link命令详解 一.ip addr命令 我是使用的linux系统是redhat7.3,其它linux的相关操作大同小异(在这里不做赘述) 1.查看 (1). ...

  3. linux sfdisk命令,Linux运维知识之Linux sfdisk硬盘分区工具程序命令详解

    本文主要向大家介绍了Linux运维知识之Linux sfdisk硬盘分区工具程序命令详解,通过具体的内容向大家展现,希望对大家学习Linux运维知识有所帮助. 功能说明:硬盘分区工具程序. 语 法:s ...

  4. linux dd iflag oflag,【转】dd命令详解及利用dd测试磁盘性能

    linux下dd命令详解 名称: dd 使用权限: 所有使用者 manpage 定义: convert and copy a file 使用方式: dd [option] dd --help info ...

  5. 【linux】循序渐进学运维-基础篇-netstat命令详解

    大家好,我是高胜寒,本文是Linux运维-循序渐进学运维-基础篇的第62篇文章 文章目录 前言 一. netstat命令详解 作用 1. 常用参数 2. 命令使用 1) 参数作用详解 2) 网络连接状 ...

  6. linux script录屏文件夹,linux下录屏和回放工具script和scriptreplay

    读书是一个长见识的过程,以前偶尔会用到录屏的工具,很少用想系统的学习一下.最近看了linux shell脚本攻略,发现很多新东西是以前自己没有接触到的.比如,这个非常好用的录屏工具:script,这次 ...

  7. Linux性能分析工具top命令详解

    top命令是linux下常用的性能分析工具,能够实时的显示系统中各个进程的资源占用情况,常用于服务端性能分析. top命令说明: top命令的结果分为两部分: 统计信息:前五行是系统的整体统计信息. ...

  8. linux系统cpu内存等资源查看top命令详解

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.top显示系统当前的进程和其他状况,是一个动态显示过程,即可以通过用户按键来不断 ...

  9. linux系统中安装和使用rz/sz命令详解

    对于经常使用Linux系统的人员来说,少不了将本地的文件上传到服务器或者从服务器上下载文件到本地,rz / sz命令很方便的帮我们实现了这个功能,但是很多Linux系统初始并没有这两个命令.今天,我们 ...

最新文章

  1. 科技公司升职的谎言与真相
  2. 在线作图|你不知道的绘制带聚类树的堆叠柱状图的方法
  3. Java知多少(87)选择框和单选按钮(转)
  4. 工业界推荐系统必读论文:基于深度学习的推荐模型——DLRM
  5. hibenate.hbm2ddl.auto属性详解
  6. J - Milking Time POJ - 3616(dp动态规划)
  7. 窥探ASP.Net MVC底层原理 实现跨越Session的分布式TempData
  8. 麻省理工告诉我们男女配对的真相!
  9. !!!SQL sever 函数表达
  10. 单代号网络图计算例题_海量优质网络图模板,轻巧实用的国产作图神器
  11. 千兆以太网芯片88E1111 RGMII模式的驱动
  12. AcWing 1123. 铲雪车 题解(欧拉回路)
  13. 实验二 单管交流放大电路
  14. git报错:index.lock File exists
  15. Python使用免费天气API,获取全球任意地区的天气情况
  16. Kamailio nats模块编译
  17. GridView使用【GridViewHelper】分组统计
  18. 关于Could not find method javacompileOptions() for arguments
  19. 520、Java Spring Cloud Alibaba -【Spring Cloud Alibaba Zuul】 2021.11.02
  20. JAVA第三方工具类

热门文章

  1. 【科普一下】量子密码就是量子通信?
  2. 求解最少翻译问题 C++
  3. 这一代墨仓式新品可能更加符合家庭用户胃口
  4. 本人独立完成的DRP分销管理系统
  5. matlab多维数据分类,选择用于高维数据分类的特征
  6. 2022电工(初级)考试题库及在线模拟考试
  7. 2022年高压电工题库及模拟考试
  8. 涉密计算机配置文件度量失败,TCP-IP练习题(有答案)
  9. 我和高中同学结婚了,在洞房花烛之夜不敢告诉他,我在客厅看到了什么
  10. loadlibrary 失败的解决方法