Ganneff's Little Blog

Thoughts of a small and very unimportant Debian Developer

By now I am a long time happy user of tmux instead of screen . I started using it somewhere in 2011 and by now I only have one usage of screen left over (direct ttyS0 access for one serial).

As usual I have a custom config for it, and if you are interested there is a link for you: tmux.conf

But the more interesting part and why I blog is that I also wrote a little helper around tmux. Ingeniously called tm. And a colleague said I should let other people use it too and place it somewhere accessible. So, err, here it is. Or better, the usage output of it, the actual script has a link somewhere below.

tmux helper by Joerg Jaspert <joerg@ganneff.de>
Call as: tm CMD [host]...
CMD is one of
ls List running sessions
s Open ssh session to host
ms Open multi ssh sessions to hosts, synchronizing input
$anything Either plain tmux session with name of $anything or
session according to TMDIR file
TMDIR file:
Each file in $TMDIR defines a tmux session.
Filename is the commandline option $anything
Content is defined as:
First line: Session name
Second line: extra tmux options
Any following line: A hostname to open a shell with in the normal
ssh syntax. (ie [user@]hostname)
Environment variables recognized by this script:
TMPDIR - Where tmux stores its session information
DEFAULT: If unset: /tmp
TMSORT - Should ms sort the hostnames, so it always opens the same
session, no matter in which order hostnames are presented
DEFAULT: true
TMOPTS - Extra options to give to the tmux call
Note that this ONLY affects the final tmux call to attach
to the session, not to the earlier ones creating it
DEFAULT: -2
TMDIR - Where are session information files stored
DEFAULT: ${HOME}/.tmux.d
TMWIN - Where does your tmux starts numbering its windows?
This script tries to find the information in your config,
but as it only checks $HOME/.tmux.conf it might fail.
So if your window numbers start at anything different to 0,
like mine do at 1, then you can set TMWIN to 1 ***

It is mainly useful for using tmux as a ssh-multiplexer and replacement for clusterssh though its happy to do any other tmux session too.

As the help text may already have told you, you can use it to list existing tmux sessions (oh, wow!). Honestly, that one is there because tm ls is shorter than tmux ls…

More interestingly, tm s HOSTNAME opens a tmux session with the first window being a ssh shell to HOSTNAME. Should you later type tm s HOSTNAME again, it will attach to the existing session.

But the real kicker for me (and colleagues) actually is the ms subcommand of it. ms being short for multi-session, it is my way of no-longer using clusterssh. Basically you type tm ms HOST1 user@HOST2 and it opens a session with one tmux window consisting of two panes. Showing you host1 and host2 at the same time. And the input to both is synchronized, that is - all your input is send to both of them. You aren’t limited to just two hosts, do as many as you wish. Actually, I have no idea if tmux has an upper limit, but I did already have more than 100 hosts open at once. The major limitation there is not tmux - but simply the available screenspace, as all hosts are shown in ONE window! (The one advantage of clusterssh, it uses one xterm per host)

This is quite helpful in all kinds of administration work involving clusters - imaging installing a package on 5 hosts of a cluster, you usually want it on all of them - or any kind of situations where one wants to run one command on many systems.

As I’m already said, I’m lazy. And so there is a way to easily define multisessions that one uses regulary, by creating files inside the directory behind $TMDIR, usually ${HOME}/.tmux.d. They are simple text files and let you easily define sessions you can then (re)open with a simple tm whateveritsname.