How do I log a gnome-terminal session to a file?

Asked by John Gruber on 2007-05-24

I converted to my Ubuntu laptop for work and am missing one key feature. I often capture ssh session logs for support reasons (dumps from routers/switches/hosts) to text files. How do I do that with gnome-terminal? All I want to do is turn logging on... capture text... turn it off... have the text file to manipulate/email etc...

Thanks in advance

John

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu gnome-terminal Edit question
Assignee:
No assignee Edit question
Last query:
2007-05-25
Last reply:
2012-09-18
Ralph Janke (txwikinger) said : #1

Thanks for the question.

You can use the command 'script' for this. Use 'man script' for the full options.

You can just start it with script <logfilename>, and end it with ctrl-d

I hope this helps.

John Gruber (john-gruber) said : #2

Thanks so much for your answer!

script is ok as log as you can afford to run it locally before you open a session. What happens if you don't need (didn't know you needed to) log a session until after you have connected to a device/host? How do you run script locally after the fact?

That's why I was interested in logging in gnome-terminal itself so logging could be turned on/off at will at the client side, not matter what you are currently connected to.

Is there a way to do this in gnome-terminal? Does gnome-terminal support any kind of plugin architecture so I could considering writing this myself?

Thanks,

John

Ralph Janke (txwikinger) said : #3

If you use konsole, you can save the history as Edit->Save History as.

You could also just cut & paste.

If you want to make sure everything stays in the history change the settings of the history with: Settings->History and set unlimited.

Jens Bech Madsen (jbmadsen) said : #4

I don't believe this is really answered. The answer at the moment would have to be that "You can't do that in Gnome Terminal".

The bug I linked is a similar feature (save scrollback buffer to file). That is in turn linked to http://bugzilla.gnome.org/show_bug.cgi?id=24457 which is really about having an option to start logging to file.

http://bugzilla.gnome.org/show_bug.cgi?id=348342 is about saving the scrollback buffer to file.

tebeka (miki-tebeka) said : #5

Just a follow-up, I've added the following lines to the top of my .bashrc, this way I have a log file for each session:

if [ -z "$UNDER_SCRIPT" ]; then
    if [ ! -d /tmp/terminal-logs ]; then
        mkdir /tmp/terminal-logs
    fi
    export UNDER_SCRIPT=1
    script -f -a -q /terminal-logs/$$.log
    exit
fi

The log for each session is under /tmp/terminal-logs/<pid>.log

HTH.

tebeka (miki-tebeka) said : #6

Ooops, remove the wrong path, should be

if [ -z "$UNDER_SCRIPT" ]; then
    if [ ! -d /tmp/terminal-logs ]; then
        mkdir /tmp/terminal-logs
    fi
    export UNDER_SCRIPT=1
    script -f -q /tmp/terminal-logs/$$.log
    exit
fi

Rafał Błaszczyk (rblaszczyk) said : #7

Here is my impoved version. It saves log to file with date, time and pid (for example 2009-03-22_20:48:09.23139.log) and compresses old logs when opening new bash shell.

if [ -z "$UNDER_SCRIPT" ]; then
        logdir=$HOME/terminal-logs
        if [ ! -d $logdir ]; then
                mkdir $logdir
        fi
        gzip -q $logdir/*.log
        logfile=$logdir/$(date +%F_%T).$$.log
        export UNDER_SCRIPT=$logfile
        script -f -q $logfile
        exit
fi

Michał Borsuk (michal-borsuk) said : #8

For future users running into the same problem: there is a build of PuTTY for Linux (GTK+), and it supports this feature (as well as many others, e.g. serial connections)

So here's the command:

sudo apt-get install putty

Good luck!

pottedmeat (bart-brobertson) said : #9

This trick makes use of the script utility. Whenever a new gnome-terminal session is opened:
- A prompt will be displayed to enter the hostname for a ssh connection.
- The tab title is set to the hostname just in case your profile on the remote host does not properly set the PROMPT_COMMAND or PS1 variables.
- The hostname is used along with a date stamp to create a filename that is used for logging the output of the script session (hostname.sessionlog.date.txt).
- The connection is made via "ssh $hostname".

This method could easily be modified to include a username in case you use ssh like "ssh user@host".

In gnome-terminal, click Edit, Profile Preferences, Title and Command tab, and put the following command into the "Custom command" field:

bash -l -c 'dt=`date "+%Y%m%dx%H%M%S"`; read -p "Hostname> " host; echo -ne "\033]0;$host\007"; script -a -f -q -c "bash -l -c \"ssh $host\"" $HOME/sessionlogs/$host.sessionlog.$dt.txt'

The above example assumes that the $HOME/sessionlogs directory already exists.

You will try it, and you will like it.

For ssh use putty, it's now available as a deb package. What some users want is to be able to save the output for further reference, so your solution does not apply.

pottedmeat (bart-brobertson) said : #11

The original question was "How do I log a gnome-terminal session to a file?" Not "when will putty be avialable?"
So, some clarification. If you want automatic session logging for gnome-terminal, then my solution absolutely applies, as I've been using it for awhile and it stores every session log into a separate file. This has proven extremely useful. If you want to enable/disable logging on the fly with a built-in feature of gnome-terminal, then the answer is "no". I can see the benefit of this, but some would argue that this is no quicker or easier than cut-and-paste. My solution is to simply log everything all the time.

# begin rant
People will have different preferences. Some people don't like putty because they prefer a terminal emulator with tabbed sessions. Putty does not have tabbed sessions unless you're using it under Windows along with one of the additional "wrapper" apps. I personally dislike putty for this very reason. I do like the background transparency and tabbed session features of gnome-terminal.
# end rant

pottedmeat: I may have misunderstood your original post: it seems that your solution does not *only* work with SSL sessions. Thanks for the script!

Nevertheless, I vote for a full and proper solution within gnome-terminal.

harrykar (harrykar) said : #13

from http://en.wikipedia.org/wiki/Terminator_%28terminal_emulator%29
"Automatic Logging - Complete logs are automatically generated of all of the user's terminal sessions."

ZTiK (of-trash) said : #14

I just registered to reply on an old thread just for the wanderers like me who are looking for a way to log an ENTIRE gnome-terminal session :

start the gnome-terminal as such :

script -q /pathto/logfile/ -c "gnome-terminal (+whatever options you want to set)"

sorry for posting in an old/closed thread, but it helped me very much :)

Can you help with this problem?

Provide an answer of your own, or ask John Gruber for more information if necessary.

To post a message you must log in.