Back to Blog

tmux - multi window session for linux

   Mariusz Antonik    General    3 min read    213 views

Tmux is very handy when working with linux

You can create session disconnect from server and then reconnect to this same session 

To easier manage session I wrote simple script which display list of available sesssions 

 

#!/bin/bash

 

# Function to display sessions and handle selection
select_tmux_session() {
    # Get list of tmux sessions and sort alphabetically
    sessions=$(tmux ls 2>/dev/null | cut -d: -f1 | sort)
    
    # Check if tmux is running and has sessions
    if [ $? -ne 0 ] || [ -z "$sessions" ]; then
        echo "No tmux sessions found or tmux server not running."
        echo "0 - Create new session"
        read -p "Enter your choice: " choice
        if [ "$choice" = "0" ]; then
            read -p "Enter new session name: " session_name
            if [ -n "$session_name" ]; then
                tmux new-session -s "$session_name"
            else
                echo "Session name cannot be empty."
            fi
        fi
        return
    fi
    
    # Display available sessions
    echo "Available tmux sessions:"
    echo "======================="
    
    # Convert sessions to array and display with numbers
    session_array=()
    counter=1
    
    while IFS= read -r session; do
        echo "$counter - $session"
        session_array+=("$session")
        ((counter++))
    done <<< "$sessions"
    
    echo "0 - Create new session"
    echo "======================="
    
    # Get user selection
    read -p "Enter your choice (0-$((counter-1))): " choice
    
    # Validate input
    if ! [[ "$choice" =~ ^[0-9]+$ ]]; then
        echo "Invalid input. Please enter a number."
        return 1
    fi
    
    # Handle selection
    if [ "$choice" = "0" ]; then
        # Create new session
        read -p "Enter new session name: " session_name
        if [ -n "$session_name" ]; then
            tmux new-session -s "$session_name"
        else
            echo "Session name cannot be empty."
        fi
    elif [ "$choice" -ge 1 ] && [ "$choice" -le "${#session_array[@]}" ]; then
        # Attach to selected session
        selected_session="${session_array[$((choice-1))]}"
        echo "Attaching to session: $selected_session"
        tmux attach -t "$selected_session"
    else
        echo "Invalid choice. Please select a number between 0 and $((counter-1))."
        return 1
    fi
}

 

# Run the function
select_tmux_session

 

 

Tags: #Guide
About the Author
Mariusz Antonik

Oracle Cloud Infrastructure expert and consultant specializing in database management and automation.

All Tags
#Advanced #Bash #bash monitoring #bash scripting #Beginner #Best Practices #Capacity Planning #cpu bottleneck #CPU Monitoring #cpu monitoring linux #cpu trends #cpu usage trends linux #cron jobs #database monitoring #database performance #detect slow queries mysql #disk capacity planning server #disk forecasting linux #Disk Monitoring #disk usage #disk usage script linux #disk usage trends #Early Detection #easy infrastructure monitoring #free-tier #Guide #Health Reporting #historical server monitoring #infrastructure #infrastructure health #infrastructure health dashboard #infrastructure health reporting #infrastructure monitoring #infrastructure monitoring report #infrastructure trends monitoring #lightweight monitoring #linux cpu usage #linux disk capacity planning #linux disk usage #Linux monitoring #linux monitoring tools #linux performance #linux performance monitoring #linux server #linux server monitoring #linux servers #linux tools #low maintenance monitoring #monitor cpu usage over time linux #monitor server trends #MySQL #mysql health reporting #MySQL monitoring #mysql optimization #MySQL Performance #mysql performance monitoring #mysql query performance issues #mysql server monitoring #mysql slow query analysis #mysql slow query monitoring #mysql trends #networking #OCI #oracle-cloud #Performance Degradation #performance monitoring #performance trend monitoring #plan disk growth server #practical server monitoring #predict disk usage growth #query optimization #Security #server health #server health reporting #server health weekly report #server monitoring #server trend analysis #server-trends #simple monitoring system #simple ops monitoring #slow queries #slow query reporting mysql #small business infrastructure #small business IT #small infrastructure monitoring #small server monitoring #storage capacity planning linux #storage monitoring #subnets #system health reporting #Trend Monitoring #Tutorial #vcn