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

Asked by John Gruber

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:
Last reply:
Revision history for this message
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.

Revision history for this message
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

Revision history for this message
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.

Revision history for this message
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.

Revision history for this message
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.

Revision history for this message
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

Revision history for this message
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

Revision history for this message
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!

Revision history for this message
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.

Revision history for this message
Michał Borsuk (michal-borsuk) said :
#10

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.

Revision history for this message
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

Revision history for this message
Michał Borsuk (michal-borsuk) said :
#12

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.

Revision history for this message
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."

Revision history for this message
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 :)

Revision history for this message
Stefan Albrecht (stefanalbrecht) said :
#15

I've been considering an option to log all activity in a terminal window. I tested a couple of terminal outputs saving all items to a file and then checked the script. But it looked like calling the "script" command had a weird determination on the shell, when I called it from the .bash_profile on Mac OSX. Is it working on OSX?
https://unix.stackexchange.com/questions/200637/save-all-the-terminal-output-to-a-file https://essaydune.com/do-my-assignment/ https://askubuntu.com/questions/690703/save-current-terminal-scrollback-to-file

Revision history for this message
pankaj Kumar (dipsha35aa) said :
#16

Green juices are made with fruits and vegetables filled with antioxidants, phytochemicals, minerals and vitamins -- sometimes as much as a day's worth of vegetables in one serving. When vegetables are juiced, the natural sugars in the vegetable are separated from the pulp, which is where the fiber is located. You may feel a "pump" of energy once those natural sugars get into your bloodstream and your glucose levels are raised.
Weight Loss
https://www.abhc.org

Revision history for this message
pankaj Kumar (dipsha35aa) said :
#17

Adding fruit to juices varies the flavor, but it comes with a price -- extra calories. If you are doing a juice fast and drinking green juices is your only source of nutrition, you can still enjoy weight-loss benefits while getting all the vitamins and minerals your body needs. The key is evenly spreading out your intake over an entire day since your body uses energy throughout the day, versus one or two servings full of calories. Packing an entire day's worth of calories into a few servings can be a recipe for weight gain and muscle damage if the body needs energy at a time when its glycogen stores are depleted.
Immunity
https://www.abhc.org/organifi-pure-review/

Revision history for this message
pankaj Kumar (dipsha35aa) said :
#18

Green juices usually start with fresh vegetables such as spinach, kale, broccoli or others as your base. In a study published in the journal "Cell" in 2011, researchers at the Babraham Institute in Cambridge, England, reported that cruciferous vegetables such as bok choy and broccoli contain a compound that boosts immunity and provides an extra layer of protection to cells in the body.
Timing Is Everything
https://resurgestore.org

Revision history for this message
pankaj Kumar (dipsha35aa) said :
#19

Since juicing vegetables removes the fiber and makes the sugar more readily available, you'll need to use that sugar wisely. Consume carbs within 30 minutes of training to restore the glucose you've used during your workout. This keeps the muscles from using protein as an energy source. Using the sugar in the green juice to replace glycogen stores that were depleted while working out means the sugar isn't stored in the body as excess glucose, which may be stored as fat if you take in more glucose than the body needs or has the capacity to store.
Considerations
https://leptoconnectstore.org

Revision history for this message
pankaj Kumar (dipsha35aa) said :
#20

Consider blending versus juicing as a means of getting fiber in your green juice.
https://sonuscompletestore.com

Revision history for this message
pankaj Kumar (dipsha35aa) said :
#21

If you're incorporating green juices into your diet as a means of improving your overall health, blending -- or pureeing -- your vegetables into a smoothie with the help of low-sugar liquids, such as water, juice or milk, may be better options for you. With blending and pureeing, the fiber stays intact. This way, you'll still receive the benefits of feeling fuller longer, which can keep you from consuming more calories in addition to those contained in your green juice.
https://diabetesfreedomstore.com

Revision history for this message
pankaj Kumar (dipsha35aa) said :
#22

Superfoods are foods — mostly plant-based but also some fish and dairy — thought to be nutritionally dense and thus good for one's health. Blueberries, salmon, kale and acai are just a few examples of foods that have garnered the "superfood" label.
https://leptitoxstore.org

Revision history for this message
pankaj Kumar (dipsha35aa) said :
#23

Superfoods don't have their own food group," said Despina Hyde, a registered dietician with the weight management program at New York University's Langone Medical Center. "As a dietician, I think 'superfood' is more of a marketing term for foods that have health benefits."
https://meticorestore.com/

Revision history for this message
pankaj Kumar (dipsha35aa) said :
#24

Consuming foods that are packed with nutrients (as many so-called superfoods are) is certainly a good idea, Hyde told Live Science. But the key to a healthy diet is to consume a variety of nutritious foods in the right quantities, she added.
Popular superfoods
https://www.iorganifireviews.com/

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.