Advanced Linux Commands for DevOps Engineers: Part-4

Unlocking the Power of tmux: From Basic to Advanced Use Cases in Production
If you’ve ever found yourself frustrated by multiple terminal windows or disconnected SSH sessions, you’ll want to get to know tmux — a terminal multiplexer that brings organization and flexibility to your command-line workflows. Whether you’re a developer, sysadmin, or DevOps engineer, tmux can help you handle complex tasks and manage multiple sessions more efficiently.
Let’s break it down, starting with basic usage and moving towards advanced production-level scenarios.
Getting Started
At its core, tmux allows you to run multiple terminal sessions simultaneously, all within a single terminal window. Let’s cover the basics:
1. Starting a tmux Session
To create a new tmux session, run:
tmux
Or, name your session so you can easily reference it later:
tmux new -s my_sessionThis starts a new session named my_session. You can now work as you would in any terminal, but with the added ability to create multiple windows and panes.
2. Creating and Navigating Panes
One of the key features of tmux is the ability to split your terminal into multiple panes:
- Split horizontally:
Ctrl + b, then% - Split vertically:
Ctrl + b, then"
After splitting, you can switch between panes with:
Ctrl + b, arrow keysYou can run different commands in each pane, allowing you to monitor logs, edit files, and run processes all from one window.
3. Detaching and Reattaching Sessions
One of the coolest features of tmux is detaching. This allows you to keep processes running in the background, even if you close your terminal or lose connection.
To detach a session:
Ctrl + b, dYou can list all your sessions with:
tmux lsAnd to reattach to a specific session:
tmux attach -t my_session
4. Basic Window Management
In addition to panes, tmux allows you to work with multiple windows within a session. You can open a new window with:
Switch between windows using:
Ctrl + b, n # Next window
Ctrl + b, p # Previous windowAdvanced tmux: Supercharging Your Workflow
Now that you’ve got the basics, let’s dive into advanced usage that can significantly improve your productivity and workflows, especially in production environments.
# Create a new session
tmux new -s session_name
# List sessions
tmux ls
# Kill a Session
tmux kill-session -t <session_name>
#Create a Session with a Command
tmux new -s <session_name> 'command_here'
# Attach to Last Detached Session
tmux attach-session -d
# Send Commands to a Specific Pane
tmux send-keys -t <session_name>:<window_number> '<command>' C-m
# Synchronize Input to All Panes
tmux setw synchronize-panes on
# Create a Vertical Split with a New Shell
tmux split-window -v
# Run a Shell Command on a New Window
tmux new-window -n <name> '<command>'
# Session Export/Import
tmux list-windows -t <session_name> -F "#{window_index} #{window_name}"1. Automating Your Workflow with tmux Scripts
In production, setting up environments with multiple panes can become tedious if done manually. Instead, you can automate this process using a simple shell script that configures your tmux sessions and panes exactly the way you like them.
Here’s an example script to set up a session for development:
#!/bin/bash
tmux new-session -d -s dev
tmux rename-window -t dev:0 'Editor'
tmux send-keys -t dev 'vim' C-m
tmux split-window -h
tmux send-keys 'htop' C-m
tmux new-window -t dev -n 'Server'
tmux send-keys 'rails server' C-m
tmux select-window -t dev:0
tmux attach-session -t devThis script:
- Creates a new session named
dev. - Opens
vimin one pane and splits the terminal to runhtopin the other. - Creates a new window for the server and starts it.
- Finally, it attaches you to the session.
Run this script, and everything is ready — no manual setup required. In production, this is particularly useful when you need to set up monitoring tools, databases, and servers all in one go.
2. Session Persistence Across SSH Connections
When you’re connected to a remote server via SSH and running critical processes (think migrations, updates, or long-running services), tmux ensures that a dropped connection won’t disrupt your workflow.
Let’s say you’re running a production database migration:
tmux new -s migrationStart the migration, then detach the session using Ctrl + b, d. If you get disconnected from the server, just log back in, and reattach the session:
tmux attach -t migration
Your migration is still running without interruption. This feature makes tmux indispensable for anyone working with remote systems or handling long-running processes.
3. Customizing tmux for Efficiency
To make tmux even more tailored to your needs, you can customize it using the .tmux.conf configuration file. Here are a few useful tweaks:
- Change the prefix key (which defaults to
Ctrl + b) to something more comfortable, likeCtrl + a:
unbind C-b set-option -g prefix C-a bind C-a send-prefix- Enable mouse support so you can resize panes and switch between windows using your mouse:
set -g mouse on- Customize the status bar to show helpful info like system load or date/time:
set-option -g status-bg colour235
set-option -g status-fg white
set-option -g status-left '#[fg=green]#S #[fg=white]'
set-option -g status-right '#[fg=yellow]#(date +"%H:%M") #[fg=cyan]#(whoami)'By customizing tmux, you can make it work exactly how you want, improving both your workflow and efficiency.
4. Tmuxinator: Automating Complex Workflows
For even more advanced setups, consider using tmuxinator, a tool that allows you to define and manage complex tmux sessions using YAML configuration files.
Here’s an example of a tmuxinator config for a project:
name: projectX
root: ~/projects/projectX
windows:
- editor:
layout: main-vertical
panes:
- vim .
- bash
- server:
panes:
- rails server
- tail -f log/development.log
- database:
panes:
- psql my_database
- htopWith a simple tmuxinator start projectX, your entire environment is set up, with pre-defined windows and panes for the editor, server, and database — all without manually splitting panes and typing commands.
5. Advanced Pane Layouts
In production, you often need to monitor several logs or processes side by side. tmux lets you predefine layouts for your panes, so you don’t have to manually resize them every time.
To cycle through layouts, you can press:
Ctrl + b, then SpaceOr use a command like:
tmux select-layout tiledThis can automatically arrange your panes in an optimized way, which is especially useful when dealing with many panes at once.
Bonus Tips:
- Resize Panes Efficiently: You can manually resize panes by holding
Ctrl + band using the arrow keys. - Use Copy Mode: Enter copy mode with
Ctrl + b, [, and useSpaceto select text, which you can then paste within tmux. - Monitor Long-Running Processes: Always run critical production commands inside tmux so they survive network interruptions or disconnections.
From the basics of splitting panes to advanced automation with scripts and tools like tmuxinator, tmux is a powerful tool that can revolutionize how you work in the terminal — whether it’s for personal development projects or managing production environments.
In production, the ability to persist sessions, automate workflows, and seamlessly manage multiple tasks makes tmux an essential tool for anyone working on remote servers or handling long-running processes. Master it, and you’ll never go back to managing terminals the old-fashioned way!
✅✅feel free to connect with us.
LinkedIn: https://www.linkedin.com/in/karthick-dkk/
Follow my Medium Account (To get valuable information)





