Trick tmux into auto-logging

Trick tmux into auto-logging

The terminal multiplexer tmux is a very useful and versatile tool. One thing that it apparently can’t do, however, is to automatically log a session to a log file. Such a feature can be very useful for interactive workloads.

Sidenote: tmux also has a plugin manager and there are logging plugins available, but they don’t do exactly what I want.

The manual way

Logging all inputs/outputs can be done with a simple command:

Manually executing this command at the creation of a pane, e.g. the start of a new session is possible but let’s be honest: I will forget it the next day already. So, an automatic method is needed.

How it should work but doesn’t

Tmux has hooks that trigger actions when certain events happen. The after-new-pane or after-new-session hooks would be ideal to execute the logging command whenever a new session or pane is opened.

Adding the hook to the config file (.tmux.conf):

Although this should work, it simply does not as others have found, too. (It is not a problem with (not) escaping the quotation marks. All my attempts with proper escaping failed also.)

How to make it work

Triggering a shell script with a hook works just fine, so the trick is put the logging command in a script:

The hook in .tmux.conf then looks like this:

Opening a new pane now automatically logs everything within that pane. The log file for each pane is named after the host (#H), session number (#S), window index (#I) and pane number (#P).

If you replicate this, please be aware of the security issues of this approach! Any content is logged to log file in plain text, including passwords or other confidential information. In the particular restricted case that I use auto-logging, this is not a problem for me.