A computer engineer poking at your cerebral cortex.

Wrangling tmux



Tmux is a terminal multiplexer which didn’t mean much to me at first. After hearing people where using tmux for multiple terminal monitoring and configuring muliple splits of terminals I had to try it. So the first thing is to install tmux on your system. I’m using Ubuntu 12.04 at this time.

sudo apt-get install tmux

Now before I go any further I need to explain some features of tmux. When you split a terminal you can see multiple terminals on 1 screen. This is called a pane. You can split a terminal in horizontal or vertical at any percentage. Each of these will be assigned a number to the pane. The confusing thing is tmux also has something called windows. Windows are like tabs in a browser. So you can’t see more then 1 window at a time. So when searching google for help make sure you are searching for pane or window depending on your needs.

Now I’m sure my use case for tmux may be different then yours. The first thing I’m going to use tmux for is bringing up multiple terminals to monitor servers. Tmux support a number of config file and configuration but I just write my own bash scripts to control tmux.

So the below snippet does a couple things. The first tmux command starts the tmux server.
-d = detach from session so we can run more commands
-s = the name of tmux session -n = the name of the current window at the bottom.

#!/bin/bash

tmux start-server
tmux new-session -d -s monitor -n htop

The below snippet splits the window vertical (-v) by 50% (-p 50). If you want to split the window horizontal use -h instead of -v

tmux split-window -v -p 50

Now lets run two command in each pane. I use a program called htop to do better monitoring then regular top. If you would like to install htop do sudo apt-get install htop. I also pass the word nice infront of these commands because usually I’m running commands on production server. Nice will turn do the priority in case the server becomes under heavy load. Nice will give priority to what is taking up resources. The second command I need to do is watch a log. The final command re-attaches to the current session so you can see it.

tmux send-keys -t monitor.0 'nice htop' C-m
tmux send-keys -t monitor.1 'nice tail -f /var/log/syslog' C-m
tmux attach-session -t monitor

So here is the script all together:

#!/bin/bash                                                                     
                                                                                
tmux start-server                                                               
tmux new-session -d -s monitor -n htop                                          
tmux split-window -v -p 50                                                      
tmux send-keys -t monitor.0 'nice htop' C-m                                     
tmux send-keys -t monitor.1 'nice tail -f /var/log/syslog' C-m                  
tmux attach-session -t monitor 

Now if you need to run a remote command on a server you can use your private/public ssh keys to remote to server and run a command.

tmux send-keys -t monitor.0 'exec ssh ubuntu@DNS -t nice htop' C-m

So now that we know how to use panes lets learn how to use windows. Windows are like tabs in firefox or chrome. You reference each window by the name and the location of the pane. You specify the number you want to use after the name of the session (monitor:1) and this window I named is logs.

tmux new-window -t monitor:1 -n logs     # new window
tmux split-window -t monitor:1 -h -p 50  # splits horizontal for better viewing of logs
tmux select-pane -t monitor:1.0
tmux send-keys -t monitor:1.0 'tail -f /var/log/syslog' C-m
tmux send-keys -t monitor:1.1 'tail -f /var/log/dmesg' C-m
tmux attach-session -t monitor 

Now you can switch between windows with ctrl-b n

You can also default to witch window you want to see by using:

tmux select-window -t monitor:0

If you need to know which pane is assigned to which number you can use ctrl-b q

Now that you know the basics of tmux here are some good templates for you to use with panes. First template is a 2x2 grid

#!/bin/bash

tmux start-server
tmux new-session -d -s monitor -n htop
tmux split-window -v -p 50
tmux select-pane -t monitor.0
tmux split-window -h -p 50
tmux select-pane -t monitor.2
tmux split-window -h -p 50
tmux attach-session -t monitor

Next one is a 3x2 grid:

#!/bin/bash

                                                                        
tmux start-server                                                               
tmux new-session -d -s monitor -n htop                                          
tmux split-window -v -p 33                                                      
tmux select-pane -t monitor:0.0                                                 
tmux split-window -v -p 50                                                      
tmux select-pane -t monitor:0.0                                                 
tmux split-window -h -p 50                                                      
tmux select-pane -t monitor:0.2                                                 
tmux split-window -h -p 50                                                      
tmux select-pane -t monitor:0.4                                                 
tmux split-window -h -p 50                                                      
tmux attach-session -t monitor 

Now you know the basics of tmux there are tons of refrences online and lots of people are using tmux for different things. Search google and I’m sure will find your info your looking for.