Have you ever started running a long process in an SSH session on a cloud server and then spent the next hour hoping nothing happens to your internet connection or SSH session before it finishes the task? WE ALL HAVE and, as more and more of our most critical servers move to the cloud, this becomes an ever-increasing point of stress in your daily workflow. One of the most overlooked utilities in the BSD/Linux toolbox is one that has been around for over a decade, but is only now truly seeing its glory recognized in the cloud computing age...it could very well save your server (and maybe even your job)! We're talking about tmux ; a Terminal Multiplexer. That may sound complicated, but the premise is simple: Treat Terminal (SSH) Sessions like individual "Panes" that aren't bound to an actual console. This not only means you can see and interact with multiple virtual terminals in a single SSH Client window, but this also means that your sessions are NOT LOST when you disconnect from the server! NOTE: While tmux is immediately available on TWM-AMI v3.x, it is NOT pre-installed on TWM-AMI v2.x. If you are unable to run the tmux command, simply run the following to download, compile, and install tmux:
SmartUpgrade install-tmux
There's a TON that tmux can do and there are plenty of folks out there who have written lots of guides, tutorials, tips, even custom themes for tmux; but we're going to focus on how tmux can save you from disaster in the cloud. At its most basic, you can launch into a new tmux session by simply running:
tmux
That said, we'd recommend getting in the habit of launching in a way that will grab an existing session if one exists or create it if it doesn't. This is how we work with "persistent" sessions:
tmux new -A -s 'MySession'
# You can put any identifying name/number you want instead of "MySession"; avoiding things like spaces and fancy characters
Initially in tmux, it looks much like a normal terminal session, spare the addition of a green info bar at the bottom. You start in a single Pane. Most tmux commands are initiated with Ctrl+b followed by the command. To add additional Panes to your Session, use Ctrl+b " to split the Window horizontally or Ctrl+b % to split the Window vertically. You'll see a new terminal session launch in the new Pane. To switch between Panes in a Window, use Ctrl+b followed by an arrow key in the direction you want to move. To close the active Pane, use Ctrl+b x and then y to confirm the yellow info bar prompt. If you close a single remaining Pane, your entire tmux Session will be terminated and you will be dropped back to the normal prompt. Now, here's were we get into the persistence part. If you want to leave your tmux Session (and anything inside it) running but disconnect from your SSH session, just close your SSH client or use Ctrl+b d to detach from the Session. You can later resume your tmux session by running that initial tmux command again while referencing the same Session name you used before:
tmux new -A -s 'MySession'
This also means you can have multiple sessions and reference them by their different names. This can be handy if you have multiple techs managing the same server. You can see a list of all tmux Sessions two ways (name appears directly before the ":" in the list and it may be a number if no name was specified); ...outside of tmux:
tmux ls
...while in tmux: Ctrl+b s # You can use the arrow keys and Enter to select a different session; ESC leaves this screen There's even more to this: multiple people/computers can share the same session in real time! That's right. You could have a single tmux Session shared by all techs managing the same server. Everyone connected to this session would have the ability to interact with it and see output in real time. This can ensure that two techs don't conflict or interfere with each other and provide better transparency/monitoring. This barely scratches the surface of all the things tmux can do and tmux is available on almost every distribution of Linux and BSD (AWS FreePBX includes tmux v1.8 pre-installed). It can save you time, headache, your server, and possibly prevent a career-ending catastrophe for you and your business. Start learning more about tmux today with the following resources:
We merely wanted to provide a quick primer on tmux and how its persistent nature can give you greater peace of mind and flexibility in working with SSH sessions. We strongly recommend you take advantage of tmux when running certain lengthy commands on your AWS FreePBX instance; such as when performing updates via SmartUpgrade or when running the cluster-install-wizard to setup your HA Cluster. NOTES:
The entire premise of tmux is such that when you detach from a session, ALL Panes within that session keep running. Unless you are using a consistent session name when launching tmux, it will always launch into a new session without disturbing any existing sessions that may be running already. This can result in additional resource consumption if you've left things like htop or other utilities running in old sessions. You should occasionally check for existing sessions with:
tmux ls
You can also kill the tmux server and all sessions currently running with:
tmux kill-server
tmux sessions do NOT persist across reboots of a server
If you want to utilize tmux every time you connect to your AWS FreePBX instance (once you have elevated to root with sudo su ), you can simply run the following SmartUpgrade command to turn on (or off) tmux-on-login:
SmartUpgrade tmux-on-login on
SmartUpgrade tmux-on-login off
It is important to note that this means that everyone who elevates to root will be immediately launched into this same Session...but you can always launch into a new Session with:
Ctrl+b : new -s "NewName"
An example tmux session with 3 Panes, each running a different command in real time: