- GNU Screen logging
- Introduction
- Log on command, shortcut Ctrl+a,H and option -L are by-and-large equivalent
- Logfile statement
- Logtstamp string
- NEWS CONTENTS
- Old News 😉
- [Sep 23, 2014] Automatic session logging and monitoring with GNU screen for the paranoid. – cmdln.org (a sysadmin blog)
- logging — Differences between Screenlog and Hardcopy on GNU Screen
- logging — How to increase the screen log size in GNU Screen
- [Sep 22, 2014] custom gnu-screen session log filename
- How do i log GNU screen output to a file
- Screen — Logging Command: hardcopy
- Remove ( color — special — escape — ANSI ) codes, from text, with sed
- Using screen for remote interaction Linux.com
- Recommended Links
- Google matched content
- Softpanorama Recommended
- Top articles
- Sites
- Top articles
- Sites
- Reference
- 18.2 log
- 18.1 hardcopy
GNU Screen logging
Introduction
When you install complex software you need log of your activities for subsequent analyses and as a record. If you work with screen then one way to get the log is to enable screen logging.
Gnu screen logging is pretty tricky. There are several command with subtle interactions between them. Interactions which are not clear after reading screen manual or manpage.
To enable logging you can can put the command log on for each window or invoke screen with the option -L (you can do it from .screenrc too). Shortcut for the log on command is Ctrl+a,shift H. If repeated it toggle logging on/off. Message screenlog.1 created appear when log is activated. If file existed message Appennding to file screenlog.1 is displayed for a shot time.
You can use command deflog on to enable logging for all screens.
Command logtstamp string string allow putting timestamps into the log. If time-stamps are turned ‘ on ’, screen adds a string containing the current time to the logfile after two minutes of inactivity. When output continues and more than another two minutes have passed, a second time-stamp is added to document the restart of the output. You can change this timeout with the second form of the command. The third form is used for customizing the time-stamp string. Default is:
Log on command, shortcut Ctrl+a,H and option -L are by-and-large equivalent
To start logging to default location (or any predefined location if command logfile is present in .screenrc use Ctrl+a,H. or command log on in your .sceenrc script. Just option -L does not start logging.
By default it will create a file with name “screenlog.0” in your home directory, to which log will be written. If you want to stop logging, use the same Ctrl+a,H
Option -L enable logging, but does not start it.
Option -L ( as in screen -L ) enables logging but does not start it
Logfile statement
logfile statement in your .sceenrc file defines location of log file for all screen windows. The syntax is:
Defines the name the log files will get. The default is screenlog.%n”.
you can customize your .screenrc file, adding for instance The second form changes the number of seconds screen will wait before flushing the logfile buffer to the filesystem. The default value is 10 seconds. For example, of connections that break often, or if you wnat to monitor logfile, you can use:
With logfile flush 1 we request that every 1 second the output be flushed to the log, making it easier to follow with tail -f.
Logtstamp string
Command logtstamp string string allow putting timestamps into the log. If time-stamps are turned ‘on’, screen adds a string containing the current time to the logfile after two minutes of inactivity. When output continues and more than another two minutes have passed, a second time-stamp is added to document the restart of the output. You can change this timeout with the second form of the command. The third form is used for customizing the time-stamp string. Default is:
You can add configuration to
/.screenrc or another configuration file named by -c filename upon invocation, and among the many options are some to enable logging what happens in the screen windows. This is useful when using screen as a reattachable daemonizer.
Consider this configuration:
That works nicely. With logfile we specify the name of the logfile, using some % escapes as per «STRING ESCAPES» in the manpage to put the date in the logfile name.
With logfile flush 1 we request that every 1 second the output be flushed to the log, making it easier to follow with tail -f.
logtstamp on turns on timestamping which writes a timestamp to the log after a default 2 minutes of inactivity. We shorten that to 5 seconds with logtstamp after 5.
Finally, log on turns on the logging.
Now, what if we want to customize the timestamp? The default looks like this:
Which the manpage says can be customized with logtstampt string . , where the default is
The manpage earlier says that arguments may be separated by single or double quotes. Doing so with the default shown doesn’t work, because a literal \n shows up in the logfile.
The solution I worked out by trial and error is that you must double-quote the string and use an octal escape value \012. Single-quoting that will output a literal backslash 0 1 2, and \n simply is not a recognized escape. Thus our final configuration directive is:
which results in output like:
If you use more than one screen window with this configuration, all windows’ output will go into the same logfile. Use the %n escape string to include a window number in the logfile name if you’d like them kept separate.
Your browser does not support iframes.
NEWS CONTENTS
- 20140923 : Automatic session logging and monitoring with GNU screen for the paranoid. – cmdln.org (a sysadmin blog) ( Automatic session logging and monitoring with GNU screen for the paranoid. – cmdln.org (a sysadmin blog), Sep 23, 2014 )
- 20140923 : logging — Differences between Screenlog and Hardcopy on GNU Screen ( Stack Overflow )
- 20140923 : logging — How to increase the screen log size in GNU Screen ( Stack Overflow )
- 20140922 : custom gnu-screen session log filename ( Stack Overflow )
- 20140922 : How do i log GNU screen output to a file ( How do i log GNU screen output to a file , )
- 20140922 : Screen — Logging ( Screen — Logging, )
- 20140922 : log ( log, )
- 210210 : Remove ( color — special — escape — ANSI ) codes, from text, with sed ( commandlinefu.com )
- 210210 : Using screen for remote interaction Linux.com ( Using screen for remote interaction Linux.com, )
Old News 😉
[Sep 23, 2014] Automatic session logging and monitoring with GNU screen for the paranoid. – cmdln.org (a sysadmin blog)
Yes its been a while since I have checked in. Sorry I’ve just been too busy. But I have a great tip this time. Recently I had the need to do automatic session logging. A 3rd party was going to be logging into one of my servers to check out some software glitches that were happening. I love using GNU Screen for many shell tasks so using it for monitoring was logical. Screen is great for several reasons. First you can detach from it so you can leave the office, go home and re-attach and not have lost your place. Second, you can share another screen. It can be shared input or you can just watch what someone else is doing. Finally screen can do native logging. I wanted to automattically launch a screen session when somone logged in so if I happened to be on the server I could monitor them in real time. I also wanted a log of the session in case I wanted to look over it later or if I was not able to monitor the session live.
I ended up adding the following to my .bashrc
Lets go through that .….
If I have some title at my terminal and if STARTED_SCREEN is set and non-null, (expands to $STARTED_SCREEN. Otherwise, expands to No.) and if SSH_TTY is set and not null, then we can attempt to create the screen.
$SSH_TTY is set when you ssh in, it should not be tripped by scp or sftp logins either.
then
STARTED_SCREEN=1 ; export STARTED_SCREEN
Here STARTED_SCREEN is set so that we dont loop on login creating a ton of screens.
if the directory is present
Attempt to reattach any unattached screens. If there are no screens to be attached then make one and attach to it.
And I added the following to my .screenrc
Most of this is self explanatory the log file for auto logging and deflog on are what give you your fun logs to look over later.
You might also want to do some logrotate on the logs or some script to expire logs that are x days old. If you forget about them over time they may try to eat your file system.
Note: I picked this up somewhere else a while back i just don’t remember exactly where. I modified it slightly to make it more readable but the credit goes to the original author. I think it was http://taint.org/wk/RemoteLoginAutoScreen.
logging — Differences between Screenlog and Hardcopy on GNU Screen
Stack Overflow
I’m new using GNU screen, and I’m using it mostly because I don’t want to lose my work if unexpectedly my SSH connection closes, and also because I want to keep a logfile of every output printed on my terminal (at least some recent tracks)
For SSH stuff, I guess Ctrl+a d to detach and screen -r or screen -r session_name to re-attach to some session solve all my problems, now I’m trying to understand logging.
I found screenlog and hardcopy on this link, which basically says
hardcopy Ctrl+a h Writes out the current display contents to the file hardcopy.n in the window’s default directory, where n is the number of the current window. This either appends or overwrites the file if it exists, as determined by the hardcopy_append command.
log Ctrl+a H Begins/ends logging of the current window to the file screenlog.n in the window’s default directory, where n is the number of the current window. If no parameter is given, the logging state is toggled. The session log is appended to the previous contents of the file if it already exists. The current contents and the contents of the scrollback history are not included in the session log. Default is off.
So, as far as I understood by this and trying to use them, after enabling screenlog, it keeps logging everything I write and all the outputs of terminal in that file (which happens to go $HOME for me) including those characters to change color like \033[1;31mm. If I do cat screenlog.0, it crashes sometimes printing the file indefinitely, but ok, I can open it with an editor or in another session.
As for hardcopy, apparently it doesn’t keep tracking commands, nor colors of outputs, and I don’t know if it takes only those few lines that are visible in terminal or everything that happened on that session, could someone clarify this to me?
Finally, I’m opening screen on Terminator instead of the classical terminal, and even if I open different screen sessions on different windows of Terminator, all logs go to screenlog.0 or hardcopy.0. In the case of screenlog, will it conflict with another process that’s being output on other sessions or overwrite it? How can I create different screenlog.n files and put them in a directory other than $HOME?
Just to conclude, which one is recommended? Sorry for this huge question, but I’m trying to write it in a way that it can be useful to clarify details of these logs that I’m having a hard time to find in other places. Feel free to correct me if I said something wrong 🙂
@WilliamPursell Sometimes I need to use it on a remote server that does not have tmux and I don’t have root permissions, so I’m stuck with screen. By the way, `tmux is simpler/better/safer or something like that? – rafa Aug 8 ’13 at 13:53
Personally, I like the way tmux handles the copy buffer. You can change panes/windows/sessions while keeping different views in different states in the scroll back, which is not possible with screen (or wasn’t the last time I looked). – William Pursell Aug 9 ’13 at 0:52
logging — How to increase the screen log size in GNU Screen
Stack Overflow
Q: Is there a way to increase the screen log size? I cannot save all my history.
$ screen -S creed1
$ command1
output command 1. (50 lines)
$ command2
output command 2. (200 lines)
$ command3
output command 3. (60 lines)
$ command4
output command 4. (75 lines)
Ctrl+A+H (saving to hardcopy.0)
vim hardcopy.0 shows only the last 100 lines approx.
I wanna know this for compliance purposes with a customer.
A: You want to do Ctrl+A + H at the beginning to start logging, not when you are done.
[Sep 22, 2014] custom gnu-screen session log filename
Stack Overflow
Q: I’d like to know if someone has tried logging their gnu-screen session to a logfile (other than the default screenlog.N where N is the session number).
$ screen -L custom_screenlog -S test_session
but screen would terminate. It’s not also in the screen manual. Tried searching Google and I can’t seem to see an answer.
A: My 4.0.2 version of GNU screen has no argument to -L switch.
The log file is systematically named screenlog.0 and created in the current directory. Then you may simply try:
screen -L -S test_session
Edit: you can customize your .screenrc file, adding for instance
logfile /tmp/screenlog-%Y%m%d-%c:%s
This way log files may be stored in a predefined directory (/tmp in this example) and may be named after logfile opening date
How do i log GNU screen output to a file
It is logged whenever there is output. The location is
/screenlog.*; you could use any number of options to copy it/rotate the logs.
Screen — Logging Command: hardcopy
( C-a h , C-a C-h )
Writes out the current display contents to the file `hardcopy. n ' in the window’s default directory, where n is the number of the current window. This either appends or overwrites the file if it exists, as determined by the hardcopy_append command.
Command: hardcopy_append state
(none)
If set to `on’ , screen will append to the `hardcopy. n ' files created by the command hardcopy ; otherwise, these files are overwritten each time.
Command: hardcopydir directory
(none)
Defines a directory where hardcopy files will be placed. If unset hardcopys are dumped in screen’s current working directory.
( C-a H )
Begins/ends logging of the current window to the file `screenlog. n ' in the window’s default directory, where n is the number of the current window. If no parameter is given, the logging state is toggled.
The session log is appended to the previous contents of the file if it already exists. The current contents and the contents of the scrollback history are not included in the session log. Default is `off’ .
(none)
Defines a directory where logfiles will be placed. If unset logfiles are written in screen s current working directory.
Remove ( color — special — escape — ANSI ) codes, from text, with sed
commandlinefu.com
Remove ( color / special / escape / ANSI ) codes, from text, with sed
Credit to the original folks who I’ve copied this command from.
The diff here is:
Theirs is supposed to remove \E[NUMBERS;NUMBERS[m OR K]
This statement is incorrect in 2 ways.
1. The letters m and K are two of more than 20+ possible letters that can end these sequences.
2. Inside []’s , OR is already assumed, so they are also looking for sequences ending with | which is not correct.
This resolves the «OR» issue noted above, and takes care of all sequences, as they all end with a lower or upper cased letter.
This ensures 100% of any escape code ‘mess’ is removed.
There are 2 alternatives — vote for the best!
Remove color codes (special characters) with sed
Handles the color codes intended for 256-color terminals (such as xterm-(256)color and urxvt-unicode-256color), in addition to the standard 16-color ANSI forms. Overkill for strict ANSI output, see other options for something simpler.
Using screen for remote interaction Linux.com
The logfile will contain the output of your session with corrections and cursor movements already evaluated and applied. One caveat is that programs that send control sequences to the screen will still confuse the output. One example of this is GNU ls, which by default colorizes output. You should turn this off in your session
Fortunately screen comes with a comprehensive logging facility that is much more sophisticated than what script can do. Screen’s logging can be turned on or off at any time with Ctrl-a H , or you can use the -L switch when starting screen to enable it by default. The log file is written to the current directory under the name screenlog.n, where n is incremented by one for each new log.
The logfile will contain the output of your session with corrections and cursor movements already evaluated and applied. One caveat is that programs that send control sequences to the screen will still confuse the output. One example of this is GNU ls, which by default colorizes output. You should turn this off in your session by using something like the following bash alias:
With that, all the pieces are in place: multiple users can share a screen session for any sort of command-line-based instruction. The teacher can at any time take control of the session by switching all other users to read-only access. Finally, you can turn on the logging facilities in screen to get an accurate and usable log of the entire session (or just portions of the session if you desire).
Recommended Links
Google matched content
Softpanorama Recommended
Top articles
Sites
Top articles
Sites
Reference
18.2 log
(none)
Same as the log command except that the default setting for new windows is changed. Initial setting is `off’.
( C-a H )
Begins/ends logging of the current window to the file screenlog. n in the window’s default directory, where n is the number of the current window. This filename can be changed with the ‘ logfile ‘ command. If no parameter is given, the logging state is toggled. The session log is appended to the previous contents of the file if it already exists. The current contents and the contents of the scrollback history are not included in the session log. Default is ‘ off ‘.
(none)
Defines the name the log files will get. The default is ‘ screenlog.%n ‘. The second form changes the number of seconds screen will wait before flushing the logfile buffer to the file-system. The default value is 10 seconds.
(none)
This command controls logfile time-stamp mechanism of screen. If time-stamps are turned ‘ on ‘, screen adds a string containing the current time to the logfile after two minutes of inactivity. When output continues and more than another two minutes have passed, a second time-stamp is added to document the restart of the output. You can change this timeout with the second form of the command. The third form is used for customizing the time-stamp string (‘ — %n:%t — time-stamp — %M/%d/%y %c:%s —\n ‘ by default).
18.1 hardcopy
— Command: hardcopy [ -h ] [ file ]
( C-a h , C-a C-h )
Writes out the currently displayed image to the file file , or, if no filename is specified, to hardcopy. n in the default directory, where n is the number of the current window. This either appends or overwrites the file if it exists, as determined by the hardcopy_append command. If the option -h is specified, dump also the contents of the scrollback buffer.
(none)
If set to ‘ on ‘, screen will append to the hardcopy. n files created by the command hardcopy ; otherwise, these files are overwritten each time.
(none)
Defines a directory where hardcopy files will be placed. If unset, hardcopys are dumped in screen’s current working directory.
Most popular humor pages:
The Last but not Least Technology is dominated by two types of people: those who understand what they do not manage and those who manage what they do not understand
Archibald Putt. Ph.D
Copyright © 1996-2021 by Softpanorama Society. www.softpanorama.org was initially created as a service to the (now defunct) UN Sustainable Development Networking Programme (SDNP) without any remuneration. This document is an industrial compilation designed and created exclusively for educational use and is distributed under the Softpanorama Content License. Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.
FAIR USE NOTICE This site contains copyrighted material the use of which has not always been specifically authorized by the copyright owner. We are making such material available to advance understanding of computer science, IT technology, economic, scientific, and social issues. We believe this constitutes a ‘fair use’ of any such copyrighted material as provided by section 107 of the US Copyright Law according to which such material can be distributed without profit exclusively for research and educational purposes.
This is a Spartan WHYFF (We Help You For Free) site written by people for whom English is not a native language. Grammar and spelling errors should be expected. The site contain some broken links as it develops like a living tree.
The statements, views and opinions presented on this web page are those of the author (or referenced source) and are not endorsed by, nor do they necessarily reflect, the opinions of the Softpanorama society. We do not warrant the correctness of the information provided or its fitness for any purpose. The site uses AdSense so you need to be aware of Google privacy policy. You you do not want to be tracked by Google please disable Javascript for this site. This site is perfectly usable without Javascript.
Источник