By default everyone prefers the history command to review/recall previously entered commands in the terminal.
But unfortunately, it only shows commands and does not show the output of previously executed commands.
There are many applications in the GUI that record terminal session activity, but very few applications are available on the command line.
The script is a great application for record your terminal session functionality on the headless server.
It is a good practice to record your terminal activity and keep it safe for further reference.
This may help you to see the detailed output if you want to investigate it sometime later.
According to this article, you need to manually run the script command if you want to record the terminal session functionality, see the following article if you want to automate it for all users.
This application provides two commands – script and scriptreplay.
What is script Command
Script is a UNIX command-line application that records a terminal session (in other words, it records everything that is displayed on your terminal).
It stores the output as text file in the current directory and the default filename is typescript
.
What is scriptreplay
This program replays a typescript, using timing information to ensure that output happens at the same speed as it originally appeared when the script was recorded.
How to Check if the script Command is Installed or not on Linux
The script is part of the Linux Core application and is already installed on most Linux distributions by default.
The script command is part of the “util-linux-ng” package on RHEL-based systems and the “bsdutils” package on Debian-based systems.
For RPM based systems
# rpm -qf /usr/bin/script util-linux-2.32.1-8.el8.x86_64 # rpm -qf /usr/bin/scriptreplay util-linux-2.32.1-8.el8.x86_64
For DEB based systems
# dpkg -S /usr/bin/script bsdutils: /usr/bin/script # dpkg -S /usr/bin/scriptreplay bsdutils: /usr/bin/scriptreplay
If you want to find out which package provides a file in Linux, see the following article.
1) How to Use script Command in Linux
If you type the script
command separately on the terminal, it will immediately start recording the current terminal function.
Once you have done the activity, type exit
to save the current record.
To start the record, type the script
command in the terminal and you will get a message like the one below.
$ script Script started, file is typescript
Let’s run few commands in the terminal to check this experiment.
The “w” command show who is logged on Linux system and what they are doing.
$ w 13:35:43 up 3:47, 1 user, load average: 1.27, 1.32, 1.43 USER TTY LOGIN@ IDLE JCPU PCPU WHAT daygeek :1 09:49 ?xdm? 2:39m 0.02s /usr/lib/gdm-x-session --run-script /usr/bin/gnome-session
The hostnamectl command used to query and change the system hostname and related settings.
$ hostnamectl Static hostname: daygeek-Y700 Icon name: computer-laptop Chassis: laptop Machine ID: xxxxx-xxxxx-xxxxx-xxxx-xxxx-xxx Boot ID: xxxxx-xxxxx-xxxxx-xxxx-xxxx-xxx Operating System: Manjaro Linux Kernel: Linux 4.19.84-1-MANJARO Architecture: x86-64
The last command show a listing of last logged in users.
$ last | head -5 daygeek :1 :1 Wed Nov 20 09:49 still logged in reboot system boot 4.19.84-1-MANJAR Wed Nov 20 09:48 still running daygeek :1 :1 Tue Nov 19 10:07 - down (17:14) reboot system boot 4.19.80-1-MANJAR Tue Nov 19 10:06 - 03:21 (17:14) daygeek :1 :1 Sat Nov 16 09:23 - down (2+17:46)
The “uname” command prints certain system information.
$ uname -a Linux daygeek-Y700 4.19.84-1-MANJARO #1 SMP PREEMPT Wed Nov 13 00:07:37 UTC 2019 x86_64 GNU/Linux
The “pwd” command outputs (prints) the current working directory.
$ pwd /home/daygeek
The free command display amount of free and used memory in the system.
$ free -m total used free shared buff/cache available Mem: 15867 3950 7722 2528 4195 9075 Swap: 17454 0 17454
speedtest-cli is a simple python script which help us to test internet bandwidth (Upload & Download) speed in Linux command line using speedtest.net. It’s using pure socket tests instead of HTTP based tests.
$ speedtest-cli Retrieving speedtest.net configuration... Testing from Bangalore Broadband Network Pvt (103.5.134.176)... Retrieving speedtest.net server list... Selecting best server based on ping... Hosted by JIFFY CABLE AND DATACOM (Bangalore) [0.10 km]: 54.121 ms Testing download speed................................................................................ Download: 25.33 Mbit/s Testing upload speed...................................................................................................... Upload: 21.46 Mbit/s
Type the exit
command in the terminal to save the script command and exit.
$ exit exit Script done, file is typescript
2) How to Check the Output of a script Command Record on Linux
Once you have performed your activity in the above step. It’s time to check it again. You can use any file manipulation command to read the output.
$ cat typescript Script started on 2019-11-20 13:35:39+05:30 [TERM="xterm-256color" TTY="/dev/pts/0" COLUMNS="211" LINES="55"] $ w 13:35:43 up 3:47, 1 user, load average: 1.27, 1.32, 1.43 USER TTY LOGIN@ IDLE JCPU PCPU WHAT daygeek :1 09:49 ?xdm? 2:39m 0.02s /usr/lib/gdm-x-session --run-script /usr/bin/gnome-session $ hostnamectl Static hostname: daygeek-Y700 Icon name: computer-laptop Chassis: laptop Machine ID: xxxxx-xxxxx-xxxxx-xxxx-xxxx-xxx Boot ID: xxxxx-xxxxx-xxxxx-xxxx-xxxx-xxx Operating System: Manjaro Linux Kernel: Linux 4.19.84-1-MANJARO Architecture: x86-64 $ last | head -5 daygeek :1 :1 Wed Nov 20 09:49 still logged in reboot system boot 4.19.84-1-MANJAR Wed Nov 20 09:48 still running daygeek :1 :1 Tue Nov 19 10:07 - down (17:14) reboot system boot 4.19.80-1-MANJAR Tue Nov 19 10:06 - 03:21 (17:14) daygeek :1 :1 Sat Nov 16 09:23 - down (2+17:46) $ uname -a Linux daygeek-Y700 4.19.84-1-MANJARO #1 SMP PREEMPT Wed Nov 13 00:07:37 UTC 2019 x86_64 GNU/Linux $ pwd /home/daygeek $ free -m total used free shared buff/cache available Mem: 15867 3950 7722 2528 4195 9075 Swap: 17454 0 17454 $ speedtest-cli Retrieving speedtest.net configuration... Testing from Bangalore Broadband Network Pvt (103.5.134.176)... Retrieving speedtest.net server list... Selecting best server based on ping... Hosted by JIFFY CABLE AND DATACOM (Bangalore) [0.10 km]: 54.121 ms Testing download speed................................................................................ Download: 25.33 Mbit/s Testing upload speed...................................................................................................... Upload: 21.46 Mbit/s $ exit exit Script done on 2019-11-20 13:41:27+05:30 [COMMAND_EXIT_CODE="0"]
3) How to Add a Custom File Name to the script Command
By default the script stores the command output in the typescript
name. You can add a custom file name for the script command output by adding a file name to the script command.
In my case, I input the file name 2g-terminal.txt
, and you can change the file name of your choice.
$ script 2g-terminal.txt Script started, file is 2g-terminal.txt
4) How to Append script Command Output to an Existing File
Running the script command without the -a
option will overwrite the output file. To add new values to an existing file without overwriting it, use the -a
option in the script command.
$ script -a 2g-terminal.txt Script started, file is 2g-terminal.txt
5) How to Save Single Command Outputs in a File Using the script Command
To store a single command output instead of an interactive shell session, use the -c
option with the script command.
For example, updating the system package comes with at least 200+ lines of output, and this option is very useful for capturing them all.
$ script -c 'yum update' magi-terminal.txt Script started, file is magi-terminal.txt Last metadata expiration check: 1 day, 12:08:03 ago on Tue 19 Nov 2019 01:53:28 PM IST. Dependencies resolved. ==================================================================================================================== Package Arch Version Repository Size ==================================================================================================================== Installing: kernel x86_64 4.18.0-80.11.2.el8_0 BaseOS 424 k kernel-core x86_64 4.18.0-80.11.2.el8_0 BaseOS 24 M kernel-modules x86_64 4.18.0-80.11.2.el8_0 BaseOS 20 M Upgrading: anaconda-core x86_64 29.19.0.43-1.el8_0 AppStream 2.1 M anaconda-gui x86_64 29.19.0.43-1.el8_0 AppStream 500 k anaconda-tui x86_64 29.19.0.43-1.el8_0 AppStream 256 k anaconda-widgets x86_64 29.19.0.43-1.el8_0 AppStream 191 k appstream-data noarch 8-20190215.el8_0 AppStream 1.8 M bind-libs x86_64 32:9.11.4-17.P2.el8_0.1 AppStream 169 k bind-libs-lite x86_64 32:9.11.4-17.P2.el8_0.1 AppStream 1.1 M . . . systemd-libs x86_64 239-13.el8_0.5 BaseOS 551 k systemd-pam x86_64 239-13.el8_0.5 BaseOS 222 k systemd-udev x86_64 239-13.el8_0.5 BaseOS 1.3 M vdo x86_64 6.2.0.298-10.el8_0 BaseOS 682 k epel-release noarch 8-7.el8 epel 21 k Installing dependencies: grub2-tools-efi x86_64 1:2.02-66.el8_0.1 BaseOS 444 k Transaction Summary ==================================================================================================================== Install 4 Packages Upgrade 171 Packages Total download size: 246 M Is this ok [y/N]: n Operation aborted. Script done, file is magi-terminal.txt
6) How to Run the script Command in Quiet Mode
Use the -q
option with the script command to suppress messages such as the script started and the script finished.
$ script -c 'whoami' -q magi-terminal.txt magi
7) How to Replay the Session Recorded Through scriptreplay Command
If you want to use the scriptreplay command to run a recorded session, make sure that you should record the session using the following format, otherwise it will not run the recorded session.
# script --timing=timing.txt session.txt Script started, file is session.txt # hostnamectl Static hostname: CentOS8.2DayGeek.com Icon name: computer-vm Chassis: vm Machine ID: e39e3a27005d44d8bcbfcab201480b45 Boot ID: 61ae1ebd0cf448f3a5927cee7cf14e0d Virtualization: oracle Operating System: CentOS Linux 8 (Core) CPE OS Name: cpe:/o:centos:centos:8 Kernel: Linux 4.18.0-80.el8.x86_64 Architecture: x86-64 # cat /etc/centos-release CentOS Linux release 8.0.1905 (Core) # cat /etc/os-release NAME="CentOS Linux" VERSION="8 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="8" PLATFORM_ID="platform:el8" PRETTY_NAME="CentOS Linux 8 (Core)" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:8" HOME_URL="https://www.centos.org/" BUG_REPORT_URL="https://bugs.centos.org/" CENTOS_MANTISBT_PROJECT="CentOS-8" CENTOS_MANTISBT_PROJECT_VERSION="8" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="8" # exit exit Script done, file is session.txt
Once you have recorded the terminal session activity using the format above, use the scriptreplay command to run the recorded session.
$ scriptreplay --timing=timing.txt session.txt
Script was one of my most used tools, twenty-five years ago, as a computer science student.
It captures std-in, std-out and std-err streams.
It also enabled capturing terminal screen escape codes. In a script session, type ‘clear’, and exit. Examine the output. Now try running vi within a script session.
Dan Stromberg,
Thank you so much to letting me know about your script.
I will give a try and update you.
FWIW, I reimplemented script(1) in python: http://stromberg.dnsalias.org/~strombrg/pypty/
It also includes a script-replay program that allows you to go forward and backward in a typescript.