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.
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.
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
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.
So here is the script all together:
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.
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.
Now you can switch between windows with ctrl-b n
You can also default to witch window you want to see by using:
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
Next one is a 3x2 grid:
#!/bin/bash
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.