Home » 2014 » June » 5 » SCRIPT command: Save your Linux sessions automatically

10:23 AM
SCRIPT command: Save your Linux sessions automatically

Share on Google+

.

script - Creates typescript or saves terminal session

FORMAT:
     script [-a] [-c COMMAND] [-f] [-q] [-t] [file]

DESCRIPTION
     Script makes a typescript of everything printed on your terminal.  It is useful for students who need a hard-copy record of an interactive session as proof of an assignment, as the typescript file can be printed out later with lpr(1).

     If the argument file is given, script saves all dialogue in file.  If no file name is given, the typescript is saved in the file typescript.

     Options:

     -a      Append the output to file specified or typescript, retaining the prior contents.

     -c COMMAND             Run the COMMAND rather than an interactive shell.  This makes it easy for a script to capture the output of a program that behaves differently when its stdout is not a tty.

     -f      Flush output after each write. This will each time create a new file overwriting previous contents

     -t      Output timing data to standard error. This data contains two fields, separated by a space. The first field indicates how much time elapsed since the previous output. The second field indicates how many characters were output this time. This information can be used to replay typescripts with realistic typing and output delays.

Note:-  Certain interactive commands, such as vi(1), create garbage in the typescript file.  Script works best with commands that do not manipulate the screen, the results are meant to emulate a hardcopy terminal.

The environment variable  SHELL is utilized by script:
 

Live Demos:  

shanky@shanky-pc:/home/shanky:> script
Script started, file is typescript
shanky@shanky-pc:~> datre
bash: datre: command not found
shanky@shanky-pc:~> date
Thu Jun  5 05:56:42 CEST 2014
shanky@shanky-pc:~> cal
     June 2014
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

shanky@shanky-pc:~> ls
04:06:14-04:26_shell.log  dead.letter              myscript.log    somesed.sed   testarg.ksh    whileLoop.ksh
04:06:14-04:34_shell.log  dfg                      perlTest        somon_output  test.sh
04:06:14-04:36_shell.log  Documents                public_html     test          tmp
abc.txt                   file_04-06-14-04:23_log  qquniq.profile  test1.sh      typescript
bin                       mylog.log                shanky.sh       test2.sh      untilLoop.ksh
shanky@shanky-pc:~> exit
exit
Script done, file is typescript

Now we can see the log file of the session here. It will be containig the same thing what we did:
 

shanky@shanky-pc:/home/shanky:> cat typescript
Script started on Thu 05 Jun 2014 05:56:35 AM CEST
shanky@shanky-pc:~> datre
bash: datre: command not found
shanky@shanky-pc:~> date
Thu Jun  5 05:56:42 CEST 2014
shanky@shanky-pc:~> cal
     June 2014
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

shanky@shanky-pc:~> ls
04:06:14-04:26_shell.log  dead.letter              myscript.log    somesed.sed   testarg.ksh    whileLoop.ksh
04:06:14-04:34_shell.log  dfg                      perlTest        somon_output  test.sh
04:06:14-04:36_shell.log  Documents                public_html     test          tmp
abc.txt                   file_04-06-14-04:23_log  qquniq.profile  test1.sh      typescript
bin                       mylog.log                shanky.sh       test2.sh      untilLoop.ksh
shanky@shanky-pc:~> exit
exit

Script done on Thu 05 Jun 2014 05:56:58 AM CEST

We can give a filename as per our choice to the log file:
shanky@shanky-pc:/home/shanky:> script -f myscript.log
Script started, file is myscript.log
shanky@shanky-pc:~> ls
04:06:14-04:26_shell.log  bin          file_04-06-14-04:23_log  qquniq.profile  test         test.sh
04:06:14-04:34_shell.log  dead.letter  myscript.log             shanky.sh       test1.sh     tmp
04:06:14-04:36_shell.log  dfg          perlTest                 somesed.sed     test2.sh     untilLoop.ksh
abc.txt                   Documents    public_html              somon_output    testarg.ksh  whileLoop.ksh
shanky@shanky-pc:~> date
Thu Jun  5 06:00:02 CEST 2014
shanky@shanky-pc:~> file shanky.sh
shanky.sh: ASCII text
shanky@shanky-pc:~> exit
Script done, file is myscript.log

And here is the content of log file:


shanky@shanky-pc:/home/shanky:> cat myscript.log
Script started on Thu 05 Jun 2014 05:59:47 AM CEST
shanky@shanky-pc:~> ls
04:06:14-04:26_shell.log  bin          file_04-06-14-04:23_log  qquniq.profile  test         test.sh
04:06:14-04:34_shell.log  dead.letter  myscript.log             shanky.sh       test1.sh     tmp
04:06:14-04:36_shell.log  dfg          perlTest                 somesed.sed     test2.sh     untilLoop.ksh
abc.txt                   Documents    public_html              somon_output    testarg.ksh  whileLoop.ksh
shanky@shanky-pc:~> date
Thu Jun  5 06:00:02 CEST 2014
shanky@shanky-pc:~> file shanky.sh
shanky.sh: ASCII text
shanky@shanky-pc:~> exit

Script done on Thu 05 Jun 2014 06:00:28 AM CEST

 

While defining the name of session log file using "script", we can use the time stamp for our ease.

Example:
 shanky@shanky-pc:/home/shanky:> script -f session_log_$(date +'%d:%m:%y-%H:%M').log
Script started, file is session_log_05:06:14-06:12.log
shanky@shanky-pc:~> exit
exit
Script done, file is session_log_05:06:14-06:12.log


Each time you want to save your session you have to start the saving session using script command. But if you want to start
saving your session automatically when you open your session, you can achieve this by adding below command into your .bashrc
file present in home directory.

test "$(ps -ocommand= -p $PPID | awk '{print $1}')" == 'script' || (script -f ~/$(date +'%d:%m:%y-%H:%M')_shell.log);

The above command will start the script automatically when you start your Linux session and when you exit you can see the saved log.
This is very useful when a user wants to save his session record automatically when logs in and out the session.

If you want to save sessions of all users on that server, you have to make changes in /etc/profile file. Add the same line at the end
of this file.

If you like it, share it !!!

 
 

Category: Open System-Linux | Views: 1969 | Added by: shanky | Tags: typescript, Script, how to save or record Linux/Gnome s, save linux sessions automatically | Rating: 0.0/0

Related blogs


You may also like to see:


[2014-09-24][Open System-Linux]
Set, unset, display shell behaviour options : SHOPT command in Linux
[2016-05-11][Open System-Linux]
An example to understand bash exit code $? in linux.
[2014-12-05][Open System-Linux]
PIDSTAT command for debugging and analysis of Performance of Linux process
[2014-10-06][Open System-Linux]
Time command in Linux to give resource usage
[2016-01-23][Open System-Linux]
Some useful examples of netstat command in Linux for network statistics

Total comments: 0
avatar