The script command in Linux makes a typescript of everything on your terminal session. It stores the information in raw form to a log file. Additionally, it also gives you the option to store information about the timing of commands. This is useful in case you want to see the realtime replay of your session. We will cover this in the later section of the tutorial.
The script command is very useful for system administration. It enables admins to look for errors by going through the session history in real-time. In this tutorial we will look at how to use the script command to record and log your terminal sessions.
Let’s get started.
How to use the Script command to log your terminal sessions?
To log your terminal sessions using the script command you need to use the following syntax :
script [filename]
Everything you enter after this command will be recorded to the file you mentioned along with the script command.
To stop the recording use :
exit
Let’s see an example :
This creates a typescript of your current terminal session under the filename ‘log‘.
View the typescript generated by the script command
After creating the typescript of the terminal session as shown above, let’s see how to view this file.
To view the file, you can use the cat command:
cat log
How to append to an existing typescript?
You can record a new session and append it to an existing file instead of generating new files To append to an existing log file use the -a flag:
script -a log
Let’s see an example. Here we are going to append to the file from our pervious example.
View the file using :
cat log
Output :
How to replay the terminal session in real-time?
To replay the terminal session, you need to record the information about the time along with the commands. To record this time log, use the -timing flag along with script command.
The syntax for that is :
script --timing=[filename1] [filename2]
Here, filename1 will contain information about time and filename2 will contain the typescript of the terminal session.
Let’s see an example.
Here, cmd_time is the file that contains the time information and realtime is the typescript of the terminal session.
We can see the contents of cmd_time using the cat command.
cat cmd_time
Output :
You can view the typescript using :
cat realtime
Output :
To replay the terminal session we will use the scriptreplay command. Scriptreplay command can playback typescripts using time information.
The syntax for that is :
scriptreplay [filename1] [filename2]
Here filename1 is the file with information about time and filename2 is the typescript.
Script-replay of the example above is shown below.
Conclusion
This tutorial was about script command in Linux. We learnt how to use the script command for creating typescripts of our terminal sessions. We also leant how to replay a terminal session by storing information about time.