Sometimes you may need to record & replay terminal session commands in Linux. You can easily do this using script and scriptplay commands. In this article, we will learn how to record & replay terminal session in Linux. You can use these commands on almost every Linux distribution since they are readily available.
How to Record & Replay Terminal Session in Linux
Generally, history command is used to view the past terminal commands. But it does not store the output of those commands. For this purpose, you need to use script command.
1. How to Record Terminal Session in Linux
Script is a useful Linux command that not only stores past commands that you executed in your terminal session but also their terminal outputs. It allows you to save all this information in a separate file that you can refer to in future. You can name these files by users who run the session, or any way as you wish. If you don’t specify any filename then script command will store all the information in file named typescript. Here is the basic syntax of script command.
# script [options] - -timing=timing_file log_filename
Here is an example to store terminal session in a file log.txt.
# script log.txt Script started, file is log.txt
As you can see, it indicates that the recording has started and that output will be stored in the file you have specified.
After this point, whatever commands you enter, and their terminal output will be stored in log.txt. It will also store all the error messages that are displayed in your terminal during this time.
Let us say you run the following command now.
$ ls file1.txt file2.txt file3.txt
Now if you want to stop recording, just enter ‘exit’
$ exit Script done, file is log.txt
If script command is unable to write to the file you specified, it will show an error message on the screen. Here is an example.
$ script script: open failed: typescript: Permission denied Terminated
You can view contents of your log.txt file using cat or any other similar command. As you can see, script command stores all commands as well as their output it in log.txt file. It records commands until exit command.
$ cat log.txt $ ls file1.txt file2.txt file3.txt $ exit Script done, file is log.txt
If you want to append terminal session commands to existing output file, you need to use -a option.
$ script -a log.txt Scripting started, file is log.txt $ date Fri Dec 17 14:59:36 IST 2021
If you want to record output of specific command but not terminal command, you can use -c for this purpose, followed by the command whose output you want to save, in quotes. Here is an example.
$ script -c 'hostname' script.log Script started, file is script.log fedingo.com Script done, file is script.log
Please note, in the above case, script command will stop recording immediately after the specified command is executed.
If you want to run script in quite mode, without displaying any messages, use -q option.
$ script -c 'who' -q script.log fedingo tty8 2021-12-17 10:45 (:0) fedingo pts/5 2021-12-17 13:42 (:0)
If you want to record the time of each command, you can use –time option and specify the file to store this information.
$ script --timing=time.txt script.log Script started, file is script.log
Now let us say you have run 4 commands on your terminal and exit scripting.
Next, when you open time.txt file, you will see output like the following.
$ cat time.txt 0.279769 306 0.047680 629 0.000005 2 0.000007 100
In the above output, the first column stands for the amount of time lapsed since last display on terminal, one for each command executed during scripting. The second column shows the number of characters displayed.
2. How to Replay Terminal Session in Linux
If you want to replay terminal session commands recorded by script command, use scriptreplay command.
$ scriptreplay log.txt
The above command will automatically replay all the commands recorded in log.txt file. But it will run the commands one after the other, without any pauses in between. If you want to run them at the same time intervals as your actual session, then you need to specify its time file also as shown below.
$ scriptreplay --timing=time.txt log.txt
In this case, the scriptreplay command will run the commands recorded in log.txt at the same intervals as they were done when the original session was recorded.
In this article, we have learnt how to record & replay terminal session in Linux.
Also read:
How to Save All Terminal Output to File
How to Remove Startup Applications in Ubuntu
How to Do Port Forwarding in Raspberry Pi
How to Install Raspbian in Raspberry Pi
How to Configure LDAP Server in Ubuntu
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.