Is there an equivalent of GNU Screen’s “log” command in tmux?

I make heavy use of screen’s “log” command to log the output of a session to a file, when I am making changes in a given environment. I searched through tmux’s man page, but couldn’t find an equivalent. Is anyone aware of a similar feature in tmux, or do I have to write my own wrapper scripts to do this?

EDIT: I’m aware of ‘script’ and other utilities that allow me to log a session. The reason that screen’s functionality is so useful is the ability to define a logfile variable which uses string escapes to uniquely identify each session.

e.g. I have a shell function which, given a hostname, will SSH to that host in a new screen window and set the window title to the hostname. When I start a log of that session, it is prefixed with the window title.

If this functionality doesn’t exist in tmux, I’ll have to create a new set of shell functions to set up ‘scripts’ of sessions I want to log. This isn’t hugely difficult, but it may not be worth the effort given that screen does exactly what I need already.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Let me see if I have deciphered your screen configuration correctly:

  • You use something like logfile "%t-screen.log" (probably in a .screenrc file) to configure the name of the log file that will be started later.
  • You use the title <hostname> (C-a A) screen command to set the title of a new window, or
    you do screen -t <hostname> ssh0 <hostname> to start a new screen session.
  • You use the C-a H (C-a :log) screen command to toggle logging to the configured file.

If so, then is nearly equivalent (requires tmux 1.3+ to support #W in the pipe-pane shell command; pipe-pane is available in tmux 1.0+):

  • In a configuration file (e.g. .tmux.conf):
    bind-key H pipe-pane -o "exec cat >>$HOME/'#W-tmux.log'"
  • Use tmux rename-window <hostname> (C-b ,) to rename an existing window, or
    use tmux new-window -n <hostname> 'ssh <hostname>' to start a new tmux window, or
    use tmux new-session -n <hostname> 'ssh <hostname>' to start a new tmux session.
  • Use C-b H to toggle the logging.

There is no notification that the log has been toggled, but you could add one if you wanted:

bind-key H pipe-pane -o "exec cat >>$HOME/'#W-tmux.log'" ; display-message 'Toggled logging to $HOME/#W-tmux.log'

Note: The above line is shown as if it were in a configuration file (either .tmux.conf or one you source). tmux needs to see both the backslash and the semicolon; if you want to configure this from the a shell (e.g. tmux bind-key …), then you will have to escape or quote both characters appropriately so that they are delivered to tmux intact. There does not seem to be a convenient way to show different messages for toggling on/off when using only a single binding (you might be able to rig something up with if-shell, but it would probably be ugly). If two bindings are acceptable, then try this:

bind-key H pipe-pane "exec cat >>$HOME/'#W-tmux.log'" ; display-message 'Started logging to $HOME/#W-tmux.log'
bind-key h pipe-pane ; display-message 'Ended logging to $HOME/#W-tmux.log'

Method 2

Here’s a tmux plugin that enables logging without messing with key bindings in .tmux.conf:

https://github.com/tmux-plugins/tmux-logging

Features:

  • prefix + P toggles pane logging. Output is cleared from unwanted ANSI characters!
  • prefix + Alt + P saves complete pane history to a file

Method 3

After looking through the documentation for tmux, I can’t find any equivalent of screen’s window logging. It looks like you’d have to use your shell functions to do what you’d like, or just use screen. You can turn on debugging, which logs both the server and client side, but it also includes a lot of extraneous tmux-related logs as well, so it wouldn’t exactly what you’re asking for.

You could possibly use tmux’s clipboard to automate saving the buffer to another session, which would be set up to accept the contents of the clipboard and save to a file. This seems kind of hackish.

Method 4

i do it using script, this is from my tmux.conf file

bind ^C new-window "script -f /home/jcosta/mydocs/work/logs/$(date '+%d%m%Y_%H%M%S')_$$.log"
bind c new-window "script -f /home/jcosta/mydocs/work/logs/$( date '+%d%m%Y_%H%M%S')_$$.log"
bind | split-window "script -f /home/jcosta/mydocs/work/logs/$(date '+%d%m%Y_%H%M%S')_$$.log"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x