I would like to run some long-lasting deployment scripts (as we do a lot with Ansible). I start the job in the evening and disconnect my ssh-session. The next day I’d like to reconnect and see the results in my session.
This can easily be achieved by “screen” running on the remote server. The much more sophisticated alternative to “screen” is “tmux”, which I therefore use.
Unfortunately some time at night my server gets shut off to save money (as it resides in public cloud). This shuts of tmux-server and all sessions information vanishes.
This article gives some hint’s how to come around that short coming.
You should know i’m running on RHEL 7.4 (at the moment and this brings tmux Version 1.8. Unfortunately i do not have a good overview of tmux-versions today. But there seems to be some 2.x versions around as well. I often run into documentation which does not suite the version i’m running.
This is tmux Version 1.8.
There is a lot to know and to write about tmux. But i do not want to repeat all that. Relevant for this article is, that a
- you can have more than one tmux-session by connecting to the server via separate terminal-sessions and start tmux separately in each one. (this might not be the main idea of tmux, BUT it is possible and needs to be thought of in the following)
- in each tmux-session you can have many windows
- in each window you can have many panes
- in each pane a separate terminal-session is running
The easiest way to get logging activated is by following command
tmux pipe-pane -o 'cat >>~/tmux_output.#S:#I-#P'
This would log the output of the current pane to the file mentioned.
While you can use following substitutions to define your filename:
- #H Hostname of local host
- #h Hostname of local host without the domain name
- #F Current window flag
- #I Current window index
- #P Current pane index
- #S Session name
- #T Current window title
- #W Current window name
- ## A literal `#’
The substitution i choose assure unique-ness on any given point in time, so you will not overwrite your output. It does not assure uniqueness over the time, so maybe you will have output of the todays session added to a complete different session, which had the same ideas before.
Maybe there is an option to add the “pipe-pane” command as hook when a new pane gets created. Unfortunately i was not able to find a full list of hooks and unfortunately hooks do not exist with Version 1.8.
The configuration in ~/.tmux.conf would maybe be similar to:
set-hook -g -n 'after-new-pane' 'run "pipe-pane -o 'cat >>~/output.#S:#I-#P'"'
… not working. There are no hooks with my version of tmux.
tmux plugin manager
The tmux-plugin manager would automate download and activation of plugins. But it requires tmux 1.9 and i’m still running 1.8. So this nice little feature will not work.
See https://github.com/tmux-plugins/tpm for explanation of how it would be installed.
The logging plugin start logging or screen capture by key stroke. Unfortunately this plugin recommends tmux version 1.9 as well. So even manual installation does not help.
See https://github.com/tmux-plugins/tmux-logging for explanation of how it would be installed.
To save (and restore) a whole session inclusive all windows and all panes, sizes etc. resurrect would help. This enables you to keep your sessions over a reboot. To save a session a key-stroke is necessary.
I do not know which version of tmux is needed. For my usecase tmux-resurrect is not suffficient as it needs to save session information without a required keystroke.
See https://github.com/tmux-plugins/tmux-resurrect for explanation of how it would be installed.
This plugin adds automated / continuous saving of sessions to tmux-resurrect. So it would exactly do what i need.
Unfortunately this plugin recommends tmux version 1.9 as well.
See https://github.com/tmux-plugins/tmux-continuum for explanation of how it would be installed.
A list of further plugins for tmux can be found here: https://github.com/tmux-plugins/
The man page of tmux i used is here:
A beginners guide with small video sequences showing things life is here:
There are loads of options to capture and save information from and within tmux. Unfortunately most of them require higher versions than i have running today. So i will stick to the first option mentioned in this article, named “pane-pipe”.