#How to Record Linux Terminal Commands With “script” – CloudSavvy IT

Table of Contents
“#How to Record Linux Terminal Commands With “script” – CloudSavvy IT”

The script
command lets you record and replay activity in your terminal. It’s helpful when you want to create a reference of the steps you’ve taken or you’re creating a tutorial for others to follow.
script
outputs a typescript of all the commands you’ve run. It works by dropping you into a sub-shell that logs everything you execute. Your session will be saved as plain text when you leave the shell. This makes script
more reliable than redirected input streams.
Basic Usage
The script
command can be run without any arguments:
script
A new capturing session will start. Your terminal session will be saved to ./typescript
in your working directory. You can specify a different file by giving script
an argument:
script example_session
Use your shell to execute the command sequence you want to record. When you’re done, press Ctrl+D
or type exit
to drop out of the sub-shell. You’ll return to your main shell session.
Now you can inspect the session log that’s been created:
cat example_session
You’ll see the output from your script
session interspersed with annotations in square brackets. These annotations are used by script
to record details about the terminal itself and the events that occur within it.
As log files are plain text you can freely manipulate them in your favorite editor. Simply cut out any lines you don’t want to retain or that others shouldn’t see.
This is useful if your session inadvertently outputs potentially sensitive information or you want to trim extraneous characters from the log. script
records everything that happens in your terminal, including backspace presses, new lines, and control characters, some of which might not be needed in the final typescript.
script
emits the name of the file it’s writing to at the start and end of your session. You can disable this output by adding the -q
flag to enable quiet mode.
The SHELL
environment variable is read when determining the shell to fork for the inner process. When the variable’s not set, script
will default to sh
.
Appending to an Existing File
It’s possible to append new commands to an existing typescript. This lets you “pause” and “resume” recording by exiting your sub-shell and starting a new session.
Add the -a
flag to append your commands to the specified file. script
will overwrite the file’s contents when the flag’s not given.
script -a example_session
Adding Timing Data
The basic typescript is ideal when you simply want to record your steps for later reading or sharing. script
can also save timing data alongside its text log. This information can be used to replay your entire terminal session with appropriate delays between the commands.
Run script
with the -t
flag to specify a path to write timings to. Timing files have a two-column format. Each entry contains the elapsed time since the last record in the first column. The second column records the number of characters that were typed, enabling accurate typing speed replays.
script -q -t example_session_timings example_session
Replaying Sessions
The scriptreplay
command reads typescripts and timing files created by script
. It replays their output into your terminal. The timing data will be used to match the duration of each typed character to its length in your original command sequence.
scriptreplay -t example_session_timings example_session
Some typescripts could contain unwanted delays or run at an uncomfortably slow speed. Use the -d
flag to accelerate the output. All recorded timings will be divided by the number you pass.
# Run 4 times faster scriptreplay -d 4 -t timings typescript
You can also use the -m
flag to cap the maximum delay between individual updates. This lets you specify the longest allowed pause in seconds before scriptreplay
will move to the next command, even if the typescript specifies a longer delay.
# Maximum delay of 2 seconds scriptreplay -m 2 -t timings typescript
Using With Interactive Commands
It’s not recommended to use script
with heavily interactive commands. Terminal text editors, curses-based applications, and dialog prompts are likely to insert polluting garbage characters into your typescript. script
focuses on capturing typical text-based output, not commands that manipulate your terminal to produce a graphical interface.
script
isn’t suitable for use with non-interactive shells as its inner shell is always interactive by nature. The command shouldn’t be expected to work with piped input either.
Other Options
The -c
flag to script
lets you specify a command. When this mode is used, script
runs the command and captures its output, instead of forking a new interactive shell. This can be more effective at capturing output from some processes that behave differently when not directly connected to a TTY.
The -f
flag instructs script
to write straight to the session log after each event. This improves safety by ensuring output is captured straightaway.
The script
manual page suggests this flag could also be used to monitor a user’s terminal in real-time, such as in a teacher and student scenario. The student would run script -f
, then the teacher would use SSH to connect and stream the typescript file live.
script
doesn’t usually accept a symbolic link as a typescript path. The --force
flag enables this behavior, instructing script
to resolve hard and soft links to their destination.
Summary
script
lets you record your terminal sessions to plain text files with accurate timing data. You can inspect, share, and print the typescripts manually, or replay them in your terminal using scriptreplay
.
The commands are included with most popular Linux distributions. They work with all terminal types but individual typescripts may not replay properly in a terminal that differs from the one used for recording. For best results, run scriptreplay
in the same terminal type you used with script
.
If you liked the article, do not forget to share it with your friends. Follow us on Google News too, click on the star and choose us from your favorites.
For forums sites go to Forum.BuradaBiliyorum.Com
If you want to read more like this article, you can visit our Technology category.