Script, record and replay terminal session activity

about script tool

In the next article we are going to take a look at Script. Is about a command line tool used to capture or record the activity of terminal sessions. After the recorded session, it can be played back using the command scriptreplay.

In the following lines we will see how to install the command line tool script and how to log terminal session activity. Generally, Gnu / Linux administrators use el history command to track what commands were executed in previous sessions. But this command does not store the command output, which can be a limitation.

As there may be some scenarios in which we want to verify the output of the commands from the previous session, to compare it with the current session. In addition we can also find some other situations in which we want save all terminal session activities for future reference. In cases like these, the script command can be useful.

Record screen with terminal and FFmpeg
Related article:
Record your desktop from the terminal with FFmpeg

Installing the Script tool on Ubuntu / Linux Mint

Script maybe is the go-to tool for recording terminal sessions. It comes installed by default in most distributions and is extremely easy to use. In case of not finding it, you can run the following command in the terminal (Ctrl + Alt + T) to install the necessary package:

util-linux installation

sudo apt install util-linux

Use the script utility

Using this tool is simple. We will only have to write the script command in the terminal (Ctrl + Alt + T) and then press Intro. This will start capture our current terminal session activity inside a file called 'typescript'.

script

For stop recording session activities, we can press Ctrl + D or type the following command and press Intro:

example script

exit

The syntax that we can use with this tool is the following:

script {opciones} {nombre-archivo}

For more information we can run help typing:

helpscript

script -h

Some examples of the script command

Let's start record our terminal session by running the script command. As mentioned above, the terminal session logs are saved in the file 'typescript'

We are going to locate this file in the working directory in which we launch the script command. We will can view file content typescript using cat / vim command.

ls typescript

ls -l typescript

Use a custom file name with the script command

Suppose we want to use our custom filename for the script command. To do so we will only have to specify file name after command. In the following example we are going to use 'session-log-(current-date-time).txt'.

script sessions-log-$(date +%d-%m-%Y-%T).txt

Then we can execute the commands we want and finish the recording by typing:

save session with custom name

exit

Add command output to script file

If we already run the script command before and the command output had already been recorded in a file called session-log.txt (for example), we may want to add the output of new sessions commands to this file. To do this we will only have to add the opción '-to' to the script command:

script -a sessions-log.txt

Once the recording is closed, we can use the cat command to see the updated logs of the session:

add multiple script

cat session-log.txt

Play recorded Gnu / Linux terminal session activity

First we are going to record the synchronization information in a file and capture the output of the commands in a separate file, this can be achieved in the script command by passing the synchronization file using the –timing option:

script --timing=timing.txt session.log

Now we will be able replay recorded terminal session activities using command scriptreplay:

play terminal session

scriptreplay --timing=timing.txt session.log

To reproduce we must use the tool scriptreplay. This will allow us to reproduce what we previously recorded. But it also has some very interesting options to improve playback. We will be able to consult these in aid that we can see by typing:

scriptreplay help

scriptreplay -h

Today we can find large options to record a terminal sessionhow could they be ascinema o termrecord. But as always, it is recommended that each user do their tests and draw their own conclusions when it comes to finding the software that best suits their needs.


Be the first to comment

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.