5

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.

rusty
  • 16,327
  • 1
    Why don't you use redirection? – Registered User Jun 23 '14 at 13:21
  • 1
    It's not about redirecting the output of some commands but it's about dumping the contents of entire session.. the inputs, outputs and error messages.. yes I can always use redirection in cases I know I need them in advance, but the Q is for the other case.. while using terminal emulator in GUI session I could just copy the contents and save them to a file, but it doesn't seem to be that easy in the mouseless world of tty.. – rusty Jun 23 '14 at 13:33
  • 2
    this answer on AU may be useful.Also read this post on U&L.Finally, have a look at google search results – Registered User Jun 23 '14 at 13:40
  • That's nice too, thanks.. But it's not exactly what I want.. – rusty Jun 23 '14 at 13:49
  • @RegisteredUser I see you included a google for me... But starting a script each time I start a terminal is not the way I want it to be. It should be post-jobs and not pre-jobs, if you get what I mean.. – rusty Jun 23 '14 at 13:58
  • as a correction update to my earlier comment, I attach this link which mentions the daemon gpm, I've not tested it myself but the console needn't always be mouseless. – rusty Jun 23 '14 at 14:14
  • AFAIK linux works with pipelines for data processing.If you want to log data, it should be piped in real time. I don't think post-job logging is possible, except manual copy-pasting. You may have some luck with finding a tool that would copy paste data from tty/terminal-emu to a file, though I think such a tool doesn't exist. – Registered User Jun 23 '14 at 14:36
  • yeah with pseudo terms like gnome-terminal that's how I used to do it, copy and paste.. but in tty there should be a way.. like you point as long as they are in the display they should be buffered somewhere temporarily and that should not only be retrieved but also in a well formatted form.. – rusty Jun 23 '14 at 15:19
  • What about http://serverfault.com/questions/526294/how-to-grab-either-bash-history-or-last-x-number-lines-from-shell-process-runnin? – Tim Aug 01 '14 at 09:08
  • that seem to display the feeds and outputs at two different terminals..?! I don't know.. I am looking to get every thing that was on my term screen before a point of time.. – rusty Aug 01 '14 at 09:18

3 Answers3

7

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.

Oli
  • 293,335
  • is there a memory limit to vcs? In the GUI I've set the terminal profile to use unlimited scroll, but that doesn't seem to apply in the ttys! – rusty Aug 01 '14 at 10:10
  • If i use sudo fold -w$(stty -F /dev/tty2 size | awk '{print $2}') /dev/vcs2 or sudo screendump (should be the same) there are only a limited output of the last commands (64 lines?), seems to be there is a memory limit. – TuKsn Aug 01 '14 at 10:25
  • +1 for fold and screendump which does the work but leaves it incomplete, just dumps the last screen.. (and yes, I too liked script but my display seems to be flicker with the screen on) – rusty Aug 01 '14 at 10:49
1

Probably you can use a program named screen. It saves all output of Terminal into a file. Have a look at this Ubuntu Forums thread http://ubuntuforums.org/showthread.php?t=1379903. CMIIW.

0

Since I cannot find any keyboard shortcut for Select All, I have found these that might help... https://stackoverflow.com/questions/1536757/selecting-text-in-terminal-without-using-the-mouse A command-line clipboard copy and paste utility?

The first one is screen, which has been suggested, but it seems to have a utility that copies text, so that might work. Enter copy mode with ^A-Esc. Start selecting text with space and end selecting text with space. Insert text with ^A-]

The second one is about pasting the text in the Clipboard to a file. I am completely unsure if any of these will work though... Hope one does.

Kristóf
  • 529