tmux for persistent sessions in Linux CLI - Time & Space Complexity
When using tmux for persistent sessions, it's helpful to understand how the time to start, attach, or manage sessions grows as you use it more.
We want to know how tmux's operations scale when handling many sessions or windows.
Analyze the time complexity of the following tmux commands.
tmux new-session -d -s mysession
# Create a new detached session named 'mysession'
for i in $(seq 1 100); do
tmux new-window -t mysession -n window$i
# Add 100 windows to the session
tmux send-keys -t mysession:window$i "echo Window $i" C-m
# Send a command to each window
done
tmux attach -t mysession
# Attach to the session
This script creates a tmux session, adds 100 windows, sends commands to each, then attaches to the session.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop creating and sending commands to 100 windows.
- How many times: 100 times, once per window.
Each new window and command adds a fixed amount of work, so the total work grows as you add more windows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 window creations and commands |
| 100 | About 100 window creations and commands |
| 1000 | About 1000 window creations and commands |
Pattern observation: The work grows directly with the number of windows you add.
Time Complexity: O(n)
This means the time to set up or manage sessions grows linearly with the number of windows or commands you run.
[X] Wrong: "Adding more windows won't affect performance much because tmux is fast."
[OK] Correct: Each window and command adds work, so more windows mean more time spent creating and managing them.
Understanding how operations grow with input size helps you explain and optimize scripts that automate session management, a useful skill in real work.
"What if we changed the script to create windows in parallel instead of a loop? How would the time complexity change?"