Monitoring your server with tmux
With tmux, you can create a monitoring system allowing you to check on your server remotely and get the perfect overview of what’s happening. Joey Bernard explains how…
The next powerful feature of tmux is the ability to take your session and detach it from the console that you are currently using. To do this, you can use the shortcut key ‘C-b d’. This puts tmux into the background, allowing you to logout of the server if you wish. The great thing is this also works if your connection simply dies, too.
Now that you have a tmux session set up that is monitoring all of the parts of your server that you are interested in, you may want to check in on it. You can log into your server and simply reattach to the existing tmux session with ‘tmux attach-session’.
There is an alternative program available called Byobu. This program is actually a wrapper around both tmux and screen. It provides a prettier interface to tmux, including a more detailed, two-line status bar at the bottom of the screen. This improved status bar will give you more information, like battery level, CPU frequency and temperature, and even whether there are updates available for your system. These extras are all configurable, and there is even the option of creating a custom notification. You should consider checking Byobu out as a ‘tmux+’ option for your monitoring setup.
Once you have your monitoring windows set up, you will likely want to name them so that they are easier to manage. You can do so with the ‘C-b ,’ shortcut. This will rename the current window, and this new name will appear in the list at the bottom of the screen.
All of the commands you have used so far to create your monitoring session manually can be done automatically through the use of a configuration file. Each of the shortcuts has an equivalent long command which can be used in the configuration file.
To create a new window, you need to add the line ‘new-window’ to the configuration file. When you create this new window, you can give it a target of a current window whose index is where your new window will be inserted.
Another important option to the ‘new- window’ command is a shell command to execute upon launching the new window. This is where you would place the command to start up ‘top’ within your new window.
Naming windows is done through the option ‘-n NAME’. This is important as it makes managing the windows easier. This name gets used to label the window, and it also gets used when you target a window with some particular command through the ‘-t TARGET’ option.
To create a pane, you will need to know which window you want to do so in. You can use the ‘split-window’ command, with either the ‘-h’ option for horizontal splitting or ‘-v’ for vertical splitting. Panes are identified through their 0-based index in the current window.
Starting tail in a pane
To start up a program in your new pane, you can add the command to the end of your ‘split-window’ tmux command. You can change this at runtime with the tmux command ‘respawn-pane -k -t TARGET-PANE command’, which will kill the current process and start up your new one.
Loading a configuration file
After all of this work, you should have a configuration file that will load your entire monitoring session. To do so, you can save it to the default filename ‘~/.tmux.conf’, or you can save it to another filename and load it with ‘tmux -f filename’.
What else can you do?
This has only been a start. You can take this and add your own monitoring programs to your tmux session to help in your system administrator duties. You can now connect to your system on a whim and see what is going on in a matter of moments.