There are two tools I use religiously at work to deal with sending commands to multiple servers at the same time. I use DSH for the quick and dirty one-liners, re-usable functions, and most things that don’t require a TTY/PTY allocation to complete. For everything else, I use TMUX.
The tmux command works on the concept of sessions, windows, and panes. A session represents a single detachable “connection” to the windows you want to work with. A window, is a single screen inside of a session. A window can be a single “pane” that takes up the entire window, or it can be multiple “panes” that break the window up into multiple pieces, each with its own “shell” instance.
While “screen” has the ability to open a serial connection and behave as a console session, much like cu, tip, or minicom, tmux does not. If you want a serial connection from a tmux pane, you’ll need to set up your pane, then call the serial connection from cu or tip (or similar.) The developer(s) didn’t want to make it anything more than just an (awesome) multiplexer. It does what it does very well, and that’s enough.
If you’ve ever worked with “screen” you know there’s a “command” prefix that needs to be passed in order to tell the multiplexer that you’re talking to it, and not just typing input to whatever pane you’re working from. In “screen” that’s “ctrl-a” by default. In tmux, it’s “ctrl-b” instead.
In order to split a screen horizontally (panes side by side,) you would do “ctrl-b %
” for example. In order to split it vertically (panes over each other,) you would do “ctrl-b "
” instead (that’s ctrl-b followed by the double quote key.) If you want to detach a session, you use “ctrl-b d
” to get back to the shell you launched the session from initially. Once it is detached, you need to know the session name. By default, the first session created is session “0” (zero.) To re-attach you pass this name to the “-t” flag like so:
tmux attach -t 0
If you need to see how many (and names of) sessions, you can use “tmux ls
” which will list each session along with some basic session information (including how many windows are in it.)
If you want to make a new window within an attached session, you can use “ctrl-b c
” to create the next available window. Split this into any panes you need to use, and set up your work flow from there.
In order to synchronize across panes, you can use the “ctrl-b :
” and then “set synchronize-panes on
” to tell it to send input to all panes simultaneously. To turn that off, do the same, but change the “on
” to an “off
,” instead.
To move from one window to the next, you can use “ctrl-b n
” and “ctrl-b p
” (for next and previous.) This lets you get to each window sequentially.
To move from one pane to the next, you can use “ctrl-b <arrow key>
” to move input focus up
, down
, left
, and right
from the pane you are currently in.
Those are the bare essentials for dealing with tmux sessions, windows, and panes on a basic level. More advanced features let you set hot keys (that may or may not require the prefix command,) change the prefix sequence, and so on. And for scripting purposes, you can run “tmux” commands from the command line outside of an attached session, to control what’s going on inside that session. This allows for some “expect” like behavior, if you’re inclined to dive deep and learn how to work with it. I may dive deeper into the more advanced stuff, and provide samples of my “.tmuxrc” configuration file later.
Do you ever confuse tmux with trex?
Nope. Never. TMUX has “long arms” and T-Rex has “short arms.” 🙂