使用script命令来记录终端会话

 

script 记录终端会话

scriptreplay 回放终端会话

script 命令解析


Usage:
 script [options] [file]

Make a typescript of a terminal session.

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
 -o, --output-limit <size>     terminate if output files exceed size
 -q, --quiet                   be quiet
 -t[<file>], --timing[=<file>] output timing data to stderr or to FILE
 -h, --help                    display this help
 -V, --version                 display version

For more details see script(1).
  1. -a 追加记录,不覆盖
  2. -c 执行命令就退出script了,不会进入交互模式(一般情况下需要exit命令退出记录)
  3. -t 记录执行命令的时间戳,之后用于回放

使用示例

简单用法

script

默认输出到typescript文件中。通过 cat 命令查看typescript文件。

记录时间戳用法,用于回放

# 记录
script -t 2>timing.log history.log
# 回放
scriptreplay timing.log history.log

这里先将时间戳输出到标准错误,然后将会话记录到文件中。否则默认输出到标准输出,会干扰正常会话。

非交互式运行

有时候我们需要将终端的输出提供给他人,如果单纯使用重定向的方式,会丢失一些终端的特性,比如颜色等。这时候可以使用script命令来记录终端会话。

script -q -c "dmesg" dmesg.log

# 查看记录
cat ls.log

参考资料

  1. Linux使用Script命令来记录并回放终端会话