script and scriptreplay in linux


script and scriptreplay are pre-installed commands in Linux. They are more like the utilities which allow the user to record their terminal session and to refer to it anytime he/she needs it. It is a command-line utility, so can run any Linux machine.

script Command

Script commad helps us to record everything printed on the terminal to a dedicated log-file. After executing the command everything present on the terminal will be recorded, be it the typing of commands or the output. When a dedicated log-file is not provided, the output is saved in the default file named typescript in the current directory.

Syntax for the script command is

$ script [OPTION] --timing=TIMING_FILE LOG-FILE

Options for script command

  • script LOGFILE : Starts the terminal recording in the specified log-file.
  • exit : This command is used when the script command is running and you wish to stop the recording.
  • script -a : On executing the command with -a option, the recording for the current session is appended with the recording of previous session in the same file, if present.

If we wish to append to a specific file, the filename can be supplied along.

  • script -c COMMAND : It is used to log the output of a single command, and needs not be exited manually. Here the execution of script stops as soon as the command has finished it's execution.
  • script -q : Runs the script command in a quiet way, displaying no starting or ending message.
  • script --timing=TIMEFILE LOGILE : Useful when we want to re-display the output stored in log-file. TIMEFILE has two columns, the first reflects the time elapsed since last display and the second reflects the number of columns to be displayed.

scriptreplay Command

scriptreplay command helps to replay information in logfile as recorded by script command. scriptreplay commans requires both timefile and logfile to work.

Syntax for scriptreplay command is

$ scriptreplay --timing=TIMEFILE logfile divisor

Options for scriptreplay command

  • -t/--timing : Specifies timescript file for the command.
  • -s/--typescript : Specifies log file.
  • -d/--divisor : Used to speed up or slow down the execution of the recorded session.
    divisor = 2 -> 2x
    divisor = 0.25 -> .25x

In scriptreplay, only the output of previously executed command is shown, the command is not re-executed.

Examples of script and scriptreply command

  1. Starting the script command with default logfile
[kshitizsaini113@localhost ~]$ script
Script started, output log file is 'typescript'.
[kshitizsaini113@localhost ~]$ 
  1. Stopping the recording from script command
[kshitizsaini113@localhost ~]$ exit
exit
Script done.
  1. Recording terminal activity via script command
[kshitizsaini113@localhost Documents]$ ls
[kshitizsaini113@localhost Documents]$ 
[kshitizsaini113@localhost Documents]$ script
Script started, output log file is 'typescript'.
[kshitizsaini113@localhost Documents]$ 
[kshitizsaini113@localhost Documents]$ pwd
/home/kshitizsaini113/Documents
[kshitizsaini113@localhost Documents]$ ls /
bin   dev  home  lib64       media  opt   root  sbin  sys  usr
boot  etc  lib   lost+found  mnt    proc  run   srv   tmp  var
[kshitizsaini113@localhost Documents]$ 
[kshitizsaini113@localhost Documents]$ exit
exit
Script done.
[kshitizsaini113@localhost Documents]$ 
[kshitizsaini113@localhost Documents]$ ls
typescript
  1. Viewing the recorded file for the above session
[kshitizsaini113@localhost Documents]$ cat typescript 

Script started on 2020-07-13 12:44:00+05:30 [TERM="xterm-256color" TTY="/dev/pts/0" COLUMNS="80" LINES="24"]
[kshitizsaini113@localhost Documents]$ 
[kshitizsaini113@localhost Documents]$ pwd
/home/kshitizsaini113/Documents
[kshitizsaini113@localhost Documents]$ ls /
bin   dev  home  lib64       media  opt   root  sbin  sys  usr
boot  etc  lib   lost+found  mnt    proc  run   srv   tmp  var
[kshitizsaini113@localhost Documents]$ 
[kshitizsaini113@localhost Documents]$ exit
exit
Script done on 2020-07-13 12:44:17+05:30 [COMMAND_EXIT_CODE="0"]
  1. Recording the output of a single command
[kshitizsaini113@localhost ~]$ script -c cal
Script started, output log file is 'typescript'.
      July 2020     
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31   
                    
Script done.
[kshitizsaini113@localhost ~]$ 
  1. Specifying our logfile and appending the output
[kshitizsaini113@localhost ~]$ script test.txt
Script started, output log file is 'test.txt'.
[kshitizsaini113@localhost ~]$ echo "Testing script command"
Testing script command
[kshitizsaini113@localhost ~]$ exit
exit
Script done.

[kshitizsaini113@localhost ~]$ script -a test.txt
Script started, output log file is 'test.txt'.
[kshitizsaini113@localhost ~]$ pwd
/home/kshitizsaini113
[kshitizsaini113@localhost ~]$ exit
exit
Script done.
  1. Specifying timefile along with the logfile
[kshitizsaini113@localhost ~]$ script --timing=time.txt log.txt
Script started, output log file is 'log.txt', timing file is 'time.txt'.
  1. Replaying the commands using scriptreplay
scriptreplay --timing=time.txt log.txt
  1. Making the replay fast using the divisor
scriptreplay --timing=time.txt log.txt 2