Using ‘script’ to record your session

This is something that I ran across recently and that I probably should have known about before.  I was doing some testing for an upcoming upgrade and was getting annoyed because I didn’t have a good way to go review the results.  I knew there had to be something out there for this though, so I did some digging.

Turns out that UNIX/Linux has a command named ‘script’ which takes care of this for you.  I did a quick check and found that it’s part of the install on FreeBSD, Mac OS X and Ubuntu Linux.  It’s probably on nearly any *nix, but those are just the ones that I checked.  Running it is real hard.


(jwood@Yardley.local)~
(0 12:33:54 504) -> script foo.txt
Script started, output file is foo.txt
(jwood@Yardley.local)~
(0 12:33:58 501) -> exit

Script done, output file is foo.txt
(jwood@Yardley.local)~
(0 12:34:01 505) -> 

The output is all logged into the file foo.txt. So that’s neat and all, but what if I sudo to root?


(jwood@Yardley.local)~
(0 12:35:49 511) -> script foo.txt
Script started, output file is foo.txt
(jwood@Yardley.local)~
(0 12:35:52 501) -> sudo -H -s
Password:
bash-3.2# cd temp/
bash-3.2# ls
.bash_profile           py-eve-test.py          zork
.bashrc                 rubygems-1.1.0          zorki.hqx
nagios-3.0.6            rubygems-1.1.0.tgz
bash-3.2# cd ..
bash-3.2# exit
(jwood@Yardley.local)~
(0 12:36:09 502) -> exit

Script done, output file is foo.txt
(jwood@Yardley.local)~
(0 12:36:12 512) -> 

Now, I to see what was recorded, I cat foo.txt.


(jwood@Yardley.local)~
(0 12:36:57 513) -> cat foo.txt 
Script started on Sun Feb  1 12:35:52 2009
(jwood@Yardley.local)~
(0 12:35:52 501) -> sudo -H -s
Password:
bash-3.2# cd temp/
bash-3.2# ls
.bash_profile           py-eve-test.py          zork
.bashrc                 rubygems-1.1.0          zorki.hqx
nagios-3.0.6            rubygems-1.1.0.tgz
bash-3.2# cd ..
bash-3.2# exit
(jwood@Yardley.local)~
(0 12:36:09 502) -> exit

Script done on Sun Feb  1 12:36:12 2009
(jwood@Yardley.local)~
(0 12:37:00 514) -> 

Perfect! It recorded everything I did. Or is it perfect? In my case, not exactly. You may notice that I have a fancy command prompt. It’s got pretty colors and everything. It seems that complicates things a bit. When I open the file with vi, the problem becomes more apparent.


(0 12:36:57 513) -> cat foo.txt 
Script started on Sun Feb  1 12:35:52 2009
^[[0;31m(^[[1;32mjwood^[[1;37m@^[[1;32mYardley.local^[[0;31m)^[[1;34m~
^[[0;31m(^[[0;31m0 ^[[0;36m12:35:52 ^[[0;32m501^[[0;31m) ^[[1;34m->^[[0m sudo -H -s
Password:
bash-3.2# cd temp/
bash-3.2# ls
.bash_profile           py-eve-test.py          zork
.bashrc                 rubygems-1.1.0          zorki.hqx
nagios-3.0.6            rubygems-1.1.0.tgz
bash-3.2# cd ..
bash-3.2# exit
^[[0;31m(^[[1;32mjwood^[[1;37m@^[[1;32mYardley.local^[[0;31m)^[[1;34m~
^[[0;31m(^[[0;31m0 ^[[0;36m12:36:09 ^[[0;32m502^[[0;31m) ^[[1;34m->^[[0m exit

Script done on Sun Feb  1 12:36:12 2009

Notice that after I sudo to root, the command prompt is simplified and the text file suddenly becomes more legible. All the stuff that I have going on in my bashrc file makes reading the text file a bit more complicated. Obviously simplifying my command prompt will make this go away.

Any how, its a pretty cool way to document what you are doing and what happened when while you were doing it. Definitely something I will be using a lot more when I’m writing documentation

Jason Wood
Latest posts by Jason Wood (see all)