Sometime we have to save our terminal activities, so that we could see and understand things performed during process for future reference. In Linux, we have one command script which Linux admins used many times for save terminal logs in a file. It is really nice tool to view our work in last when it get finish. In this post we would see HowTo Record and Replay Linux Terminal Sessions using ‘script’ and ‘scriptreplay’ Commands.

Setup

# script -V
script from util-linux 2.27.1

# scriptreplay -V
scriptreplay from util-linux 2.27.1

# uname  -r
4.4.0-96-generic

# lsb_release -d
Description:	Ubuntu 16.04.3 LTS

To Start recording on for terminal, we use script command like mention below.

# script 
Script started, file is typescript

As we didn’t mentioned any file name, script command will automatically create file named typescript and start recording terminal logs in same.

But it is always better to create your own file for terminal recording.

# script terminal_log
Script started, file is terminal_log

When we need to quit recording, we can easily do so with exit type on command prompt.

# exit
exit
Script done, file is terminal_log

We can see these log files and realize recording log with vim or cat command like mentioned below

root@srv:~# cat terminal_log 
Script started on Wednesday 11 October 2017 02:26:20 AM IST
root@srv:~# w
 02:26:25 up 1 day, 14:19,  1 user,  load average: 0.01, 0.07, 0.08
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
ssirohi  tty7     :0               Mon12   38:19m 22:13   0.36s /sbin/upstart --user
root@srv:~# uptime
 02:26:35 up 1 day, 14:19,  1 user,  load average: 0.09, 0.09, 0.09
root@srv:~# date
Wed Oct 11 02:26:59 IST 2017
root@srv:~# exit
exit

Script done on Wednesday 11 October 2017 02:27:31 AM IST

There are many other option used with script command for facilitate with more options like mentioned below.

-a --append   -- With this option, we can append on same file, either we need to mention file name for append or it will automatically append to default typescript

-c --command  -- This is used to capture command output inside terminal log, either we need to mention file name for append or it will automatically append to default typescript

-q --quiet    -- With this option it will not show any start and stop message on terminal while start or exit script command.

-f --flush    -- This option is used to see interactive output of terminal log file, with this option we can continually monitor terminal log from another terminal.

-t --timing   -- This is really and helpful option used to manage to see terminal logs like replay realistic typing and with its delay, It look like video play of terminal without audio.

Let’s look all options with examples.

-a –append

This option is used to append terminal log file. In case we didn’t mention any terminal log file, it will try to append typescript file.

# script -a 

# script -a terminal_log

In below mention log we had try to work on default terminal log file typescript.

root@srv:~# script terminal_log
Script started, file is terminal_log
root@srv:~# uptime
 02:44:05 up 1 day, 14:37,  1 user,  load average: 0.11, 0.05, 0.03
root@srv:~# exit
exit
Script done, file is terminal_log

root@srv:~# script -a
Script started, file is terminal_log
root@srv:~# cal
    October 2017      
Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7  
 8  9 10 11 12 13 14  
15 16 17 18 19 20 21  
22 23 24 25 26 27 28  
29 30 31              
root@srv:~# exit
exit

-c –command

This options is used to capture command output in script terminal log as mention below.

root@srv:~# script -c w terminal_log
Script started, file is terminal_log
 04:10:14 up 1 day, 16:03,  1 user,  load average: 0.00, 0.01, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
ssirohi  tty7     :0               Mon12   40:03m 23:10   0.36s /sbin/upstart --user
Script done, file is terminal_log

root@srv:~# cat terminal_log 
Script started on Wednesday 11 October 2017 04:10:14 AM IST
 04:10:14 up 1 day, 16:03,  1 user,  load average: 0.00, 0.01, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
ssirohi  tty7     :0               Mon12   40:03m 23:10   0.36s /sbin/upstart --user

Script done on Wednesday 11 October 2017 04:10:14 AM IST

-q –quiet

sometime we don’t want to print start and stop message of script command, this options is used to work for same.

Start script without start message
root@srv:~# script -q terminal_log

root@srv:~# date
Wed Oct 11 04:20:38 IST 2017
Stop script without stop message
root@srv:~# exit
exit

-f –flush

This is real important option in case we like to analyze terminal logs from another terminal like we see real time log with tail -f in log file.

root@srv:~# script -f terminal_log
Script started, file is terminal_log

 We can monitor terminal log file from another terminal session

# tail -f terminal_log

-t –timing

This is my favourite way to capture terminal log in file because we can replay realistic typing with correction and delay used at recorded time.

# script --timing=time_log terminal_log
Script started, file is terminal_log

 We can replay terminal_log like below mentioned way

# scriptreplay --timing=time_log terminal_log

So here we have another command used to replay script terminal log.

Script and Scriptreply commands used to record terminal log in system file which could later analyze for any leaning or reference purpose.