script and scriptreplay commands in Linux
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
In this article we discuss how to use the script command to record all terminal activities and scriptreplay to playback the recorded activities.
Table of contents.
- Introduction.
- Syntax.
- Commands.
- Summary.
- References.
Introduction.
The script command is used to create a typescript file of every command executed on the terminal.
It is used by users who need to log their sessions as proof of work done on the system e.g students. The file generated by the executed commands can also be used to create a script so as not to repeat the process again while performing the same actions. It can also be used by system administrators for logging during installations, configurations or troubleshooting sessions.
Syntax.
The syntax is as follows,
script [OPTIONS] [FILE]
Commonly used options include,
- -a, to append new commands/output to a file.
- -q, to run the command quietly without starting and stopping notifications.
- -T, to save timing information for playback.
- -c, to execute a command and log its output instead of a normal interactive shell
Commands.
Recording starts when the script command is executed and stop recording/logging when Ctrl+D or exit is pressed or executed. Everything in between these two commands is saved into a specified file or a typescript file.
- To get started we can execute the command without any arguments as follows,
$ script
Script started, output log file is 'typescript'.
$ mkdir TEST
$ cd TEST/
$ touch file{1..3}.txt
$ ls
file1.txt file2.txt file3.txt
$ cat > file1.txt
This is a test
^C
$ cat file1.txt
This is a test
$ rm -rf *.txt
$ cd ..
$ rm -rf TEST/
$ exit
exit
Script done.
$ ls
typescript
As you can see from the output of ls command, a new file typescript was created, it contains all the previously executed commands. We can concatenate it to view details.
$ cat typescript
# output
Script started on 2022-01-07 03:00:53+03:00 [TERM="xterm-256color" TTY="/dev/pts/1" COLUMNS="123" LINES="41"]
$ mkdir TEST
$ cd TEST
$ touch file{1..3}.txt
$ ls
file1.txt file2.txt file3.txt
$ cat > file1.txt
This is a test
^C
$ cat file1.txt
This is a test
$ rm -rf *.txt
$ cd ..
$ rm -rf TEST/
$ exit
exit
Script done on 2022-01-07 03:04:23+03:00 [COMMAND_EXIT_CODE="0"]
From output notice the 'Script started' and 'Script done' statements which mark the start and ending of the recording.
- We can also execute script command and save the output to a defined file as follows,
script cmd.txt
Now all output will be redirected to cmd.txt file.
- To avoid creating another file or for continuation we can use the -a option to append all output to a previous file. For example if we wish to continue the previous session and place the output in cmd.txt file, write,
script -a cmd.txt
- The -c option executes a command and logs the output to a file,
script -c ls ls.txt
The file ls.txt will contain the output from ls command.
This is also applicable when we are executing a script and need to log its output for later reference, for example if we have the shell script shell.sh we can write,
script -c ./shell.sh out.txt
The output from the shell script will be saved in out.txt file.
- The -q option is used to execute script quietly, that is, there will be not starting and stopping notifications when we start or stop script command,
script -q
- We can create shell scripts from using the script command by separating the inputs(stdin) and outputs(stdout) using the -I option to specify an input file which will log all commands(stdin) and -O option to specify an output file which logs stdout from the commands,
script -I input -O output
where input will hold commands excluding the output and output file will hold output from the commands.
We can then use the input file to create a shell script.
- We use the -T command to capture terminal activity step by step for later playback using the scriptreplay command.
script -T time
The timing log file time is where all logs will be stored for playback using the script replay command.
After executing different commands on the terminal, we can replay them back as follows,
scriptreplay -t time
This is useful as a teaching tool for new Linux users or for students to submit their assignments.
Summary.
The script command makes a typescript file of everything we execute in the terminal, it is useful in cases when we need to rerun commands or remember what we had previously executed, it also has the options to separate stdin from stdout which is useful since we can use stdin to create scripts.
The script command can be used by a system administrator during configuration or installation to store the commands so that later he/she can create a script and automate the work during the next configuration.
scriptreplay can be used as a teaching tool whereby an instructor can record commands and use the file to teach new Linux students or by students to submit assignments.
We could also use the history command to view previously executed commands.
References.
- man script or man scriptreplay for the manual pages.
- script --help or scriptreplay --help for other command options.
- history command in Linux.
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.