Ubuntu – How to save the contents of unlogged console screen to a file

command lineservertty

I'm on a tty, with a lots of command inputs, bulk of verbose their output, and some error messages. Now I have to get all those stdin/stdout/stderr texts, in the format displayed in the console screen, dumped to a file. How can I get that done using the inbuilt tools (preferably)?

I don't know if the question is confusing! It's really a simple one.. Suppose I logged into tty1 console terminal, ran some commands (with no logging enabled, because I didn't feel the need to) but suddenly there came certain outputs/messages that I want to dump into a file (or the other option get a pen and a paper).

The reason why I am assuming this could be done is because you can use the shift + PgUp/PgDwn to shift the screen outputs, which means they are still there in the buffer (even if the processing is pipelined)! ..and that is what should be recovered.

Best Answer

  • TTYs use "virtual console memory" devices to buffer their screen content. You can read more about them in man vcs but this will let you get what is on the screen at the current moment.

    In practise these are just numbered files in /dev/ that line up with the TTY number. Here's an example I did with TTY2:

    $ sudo fold -w$(stty -F /dev/tty2 size | awk '{print $2}') /dev/vcs2
    
    Ubuntu 14.04 LTS bert tty2                                                      
    
    bert login: oli                                                                 
    Password:                                                                       
    oli@bert:~$ cd test                                                             
    oli@bert:~/test$ ls                                                             
    Madonna - 10 - Bedtime Story.mp3  output_MP3WRAP.mp3                            
    Madonna - 11 - Take A Bow.mp3                                                   
    oli@bert:~/test$                                       
    

    The fold -w$(...) there is because the buffered output doesn't appear to have the control characters or newlines I would expect. This simply adds \n at the end of every line.

    As TuKsn points out in the comments, you don't have to mess around with all this, you can achieve exactly the same with:

    sudo screendump 2
    

    You can stick > tty.log on the end of either command to write the output to a file called tty.log in the current directory:

    sudo screendump 2 > tty.log
    

    Again, this will only get you what is on the screen. Even if you increase the scrollback buffer in TTYs, this isn't stored in accessible memory. You can alter that but that involves recompiling the kernel.

    That would involve rebooting and losing the current screen so if you can do that there are much easier options for logging future IO, like screen or tmux or just script.