In this guide, we are going to look at how to use a script and scriptreplay commands in Linux that can help you to record commands and their output printed on your terminal during a given session.
The history command is a great command-line utility that helps users to store previous commands used, though it does not store the output of a command.
Therefore the script command comes in handy to provide you a powerful functionality that helps you to record everything that is printed on your terminal to a log_file.
You can then refer to this file later on in case you want to view the output of a command in history from the log_file.
You can also replay commands that you recorded using the scriptreplay command by using timing information.
How to Record a Terminal in Linux?
If you want to create a typescript (record of a terminal session) in Linux, you can use the script command which starts a new shell session and records everything displayed on the terminal, including input and output, to the specified filename.
script my_terminal_session.txt
After running the script command, you will see the terminal prompt, where you can run various commands and your terminal session will be recorded to the “my_terminal_session.txt” file.
Now try to execute a few commands to allow the script to record executed commands on the terminal.
cal w uptime whoami pwd
To stop recording, simply type exit or press Ctrl+D.
exit
Now try to view the log file ‘my_terminal_session.txt‘ for all recorded commands, while you view the log you realize that the script also stores line feeds and backspaces.
vi my_terminal_session.txt
You may use the -a
option to append the log file or typescript, retaining the prior contents.
script -a my_terminal_session.txt vi my_terminal_session.txt
To log the results of a single command other than an interactive shell session, use the -c
option.
script -c 'hostname' script.log
If you want the script to run in a quiet mode then you can use the -q
option. You will not see a message that shows the script is starting or exiting.
script -c 'who' -q script.log
To set timing information to standard error or a file use the --timing
option. The timing information is useful when you want to re-display the output stored in the log_file.
Let us start the script and run the following commands w, uptime, and cal to be recorded.
script --timing=time.txt script.log
You can view the script.log and time.txt file for the timing command above.
vi script.log
Now view the time.txt file.
vi time.txt
The time.txt file has two columns, the first column shows how much time has elapsed since the last display and the second column, shows the number of characters that have been displayed this time around.
Use the man page and --help
to seek more options and help in using the script command-line utility.
How to Replay Recorded Terminal Session in Linux?
The scriptreplay command helps to replay information in your log_file recorded by the script command.
The timing information is defined by the -timing=file
option used with the script command and the file in this case is script.log which was used with the script command.
scriptreplay --timing=time.txt script.log
When the log_file is replayed using the timing information, the commands recorded are run and their output is displayed at the same time the original output was displayed while being recorded.
Summary
These two commands, script and scriptreplay easy to use and help a lot when you need to run the same batch of commands several times.
They help a lot in managing servers that have only a command-line interface for interaction with your system. Hope this guide was useful and if you have anything to add or face a challenge while using them, do not hesitate to post a comment.
Hello
Is there any way to eliminate the ^m and other special characters from the output file? I think those characters are related to the screen color which gets appended before each character with a different color.
For those trying to get this working on macOS 10.13.x I was able to do,
and then to playback the recording with `script`
Thanks for the blog on script command. It was very helpful and elaborative.
@abhi
You are most welcome, thanks for reading through and for the feedback.
Small typo:
The scriptreplay command helps to replay information in your log_file used with the script command.
@Zorglups
Thanks for the heads up, we will correct it.
hello, cool article :)
i find a funky typo -> You will not see a massage that shows script is starting or exiting.
you write “massage” instead of “message”
peace
@Jonathan,
Thanks a lot man, corrected in the article…:)
How do I append a session of terminal & do scriptreplay with the append session, I am trying like this:
When I ran this first time and save the session I can see the replay through:
but when I want to add or append more session to the same script then I am not able to run properly the same script with append one through the same command
Please help me out!!
@Shashank
Try to specify the log file in front of the -a flaf like this:
script –timing=1.timing -a 1.session
Hi,
How to call function in script command and record the terminal output in log file?
@Dipak
Simply start script command and run function as any other command, it will save the output to the script command file as explained in the article.
Hi Aaron,
I’m unable to direct the timing information in a file.
script --timing=filename.txt script.log
script: invalid option — ‘-‘
usage: script [-a] [-f] [-q] [-t] [file]
Could you please help out.
@sahil
check the timing option, it should be – -timing=filename.txt not -timing=filename.txt
Or use -t filename.txt
Hi Aaron,
I’m unable to get the time information directed to the time file.
[ ~]#script –timing=filename.txt script.log
script: invalid option — ‘-‘
usage: script [-a] [-f] [-q] [-t] [file]
#script –version
script (util-linux-ng 2.17.2)
Could you please help me out. I’m using centOS 6.
Getting this error, any idea? It dies immediately.
$ script
Script started, file is typescript
wsh: invalid option — ‘i’
error=3 Invalid parameter
usage: wsh [-d][ -c ][ -p ][ -s ]
Sets login environment and spawns an interactive server.
-d enable debugging
-c set a single executed command
-p set the configuration full path
-s set the shell full path
Script done, file is typescript
@SV
I have not seen this error before, but try to use a different log_filename apart from the default and see what happens.
This could be a possible cause of the error:
I have looked around for info about the error, according to what i found,
wsh
from: https://github.com/chenyf/wsh is used to execute command in a Linux container through unix socket.There could be certain configuration issues to do with
wsh
on your machine causing the error.How we can remove junk characters in ouput, No clear information in output.
That is the default output behavior , those characters can not be removed.
Are there specialized services or tools which convert those log and timing file to a gift similar to yours?
how to install those commands on ubuntu ? -_-
install the package that provides the utilities by running this command (sudo apt-get install bsdutils)
I wrote pypty, which is a pure-python reimplementation of script(1). It includes script-replay (note the dash), which allows you to step through your scripts with arrow keys (curses) or buttons (GObject Introspection).
Thanks for the info @Dan Stromberg, how can install the pypty utility
When We are using –timing=time.txt It does not working It Does not show No such file or directory
@kunalsing You need to use the – -timing=filename.txt option with script first to create the timing information file, you can use any other name apart from time.txt, then use
– -timing option with scriptreplay ie – -timing=filename.txt . The filename used with script must be the same as the one used with scriptreplay.
I find these kind of tools very useful for tutorial purposes.
Next to script there’s also ttyrec (available thru package manager on almost any distro).
Thanks for the info, i will try to write about ttyrec.
How did you generate this gif file?
@abi
We use custom built scripts for doing that.