Back to Blog

tmux - multi window session for linux

   Mariusz Antonik    General    3 min read    370 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 #agent-visibility #alerts #amazon-linux-2023 #argo-cd #auditd #automation #backend-infrastructure #backup-verification #bandwidth-monitoring #bare-metal-server #Bash #bash cpu monitoring script #bash monitoring #bash scripting #bash-scripts #Beginner #Best Practices #block volume backup #Capacity Planning #centos-ftp-migration #centralized-logging #cloud backup strategy #cloud-costs #cloud-database-setup #cloud-networking #cloudflare-workers #compute #container-monitoring #control-panel-security #cpu bottleneck #CPU Monitoring #cpu monitoring linux #cpu monitoring script linux #cpu trends #cpu usage trends #cpu usage trends linux #cpu-monitoring-without-tools #cpu-performance-decline-server #cpu-performance-degradation-linux #cpu-usage-history-linux #create oracle db system in oci #cron #cron cpu monitoring #cron cpu monitoring linux #cron jobs #cron-monitoring #custom-linux-distribution #database #database monitoring #database performance #database-health #database-setup #debian #detect slow queries mysql #devops #devops-checklist #devops-help #disk capacity planning server #disk forecasting linux #disk growth trend linux #Disk Monitoring #disk usage #disk usage script linux #disk usage trends #disk-capacity #disk-saturation-detection-linux #Early Detection #easy infrastructure monitoring #elasticsearch #fail2ban #field-server-checklist #firewall-rules #fleet-ops #free-tier #gitops-security #Guide #health dashboards #Health Reporting #historical server monitoring #how to monitor cpu usage linux #https-certificates #infrastructure #infrastructure health #infrastructure health dashboard #infrastructure health reporting #infrastructure monitoring #infrastructure monitoring report #infrastructure trends #infrastructure trends monitoring #Infrastructure Visibility #infrastructure-automation #infrastructure-checklist #interview-prep #ip-allowlist #journald #kubernetes-security #lightweight linux monitoring #lightweight monitoring #lightweight-monitoring-solution #linux #linux administration #linux cpu monitoring #linux cpu usage #linux disk capacity planning #linux disk usage #Linux monitoring #linux monitoring setup #linux monitoring tools #linux performance #linux performance monitoring #linux server #linux server monitoring #linux servers #linux storage #linux tools #linux-admin #linux-disk-monitoring #linux-hotspot #linux-monitoring-for-small-business #linux-networking #linux-performance-tuning #linux-remote-desktop #local-dns #log-management #log-retention #logrotate #loki #low maintenance monitoring #mkcert #monitor cpu usage over time linux #monitor linux server health #monitor server trends #monitor small production server #monitoring #monitoring without complexity #monitoring-without-devops-team #MySQL #mysql health reporting #MySQL monitoring #mysql optimization #MySQL Performance #mysql performance degradation #mysql performance monitoring #mysql performance trends #mysql query performance issues #mysql server monitoring #mysql slow queries #mysql slow query analysis #mysql slow query monitoring #mysql trends #mysql-health #mysql-heatwave #mysql-monitoring-lightweight #mysql-workload-trends #networking #networkpolicy #nsg #OCI #oci backup #oci bastion tutorial #oci block volume #oci infrastructure as code #OCI monitoring #oci networking #oci oracle database private subnet setup #oci oracle database tutorial #oci security #oci setup guide #oci terraform tutorial #oci tutorial for beginners #oci vcn terraform #oci virtual machine db system guide #oci-database #oci-mysql-heatwave #oci-mysql-heatwave-tutorial #oci-subnets #offline-pwa #operations-checklist #oracle base database service tutorial #oracle cloud bastion #oracle cloud free tier tutorial #oracle cloud infrastructure step by step #oracle cloud infrastructure tutorial #oracle cloud storage #oracle database on oci setup #oracle-cloud #oracle-cloud-mysql-database-service #oracle-cloud-mysql-setup #oracle-cloud-vcn-setup #patch-management #path-mtu-discovery #Performance #Performance Degradation #performance monitoring #performance trend monitoring #performance trends #plan disk growth server #plesk #practical server monitoring #predict disk usage growth #private instance access #proxmox #query optimization #remote-workstation-security #rhel-tuned #rollback #route-tables #rsyslog #Security #security lists #security-monitoring #selinux #server #server health #server health reporting #server health weekly report #server monitoring #Server Performance #server trend analysis #server-hardening #server-health-checklist #server-security #server-security-checklist #server-throughput #server-trends #server-troubleshooting #servers #service-worker #siem #simple cpu monitoring linux #simple linux monitoring #simple monitoring small business #simple monitoring system #simple ops monitoring #slow queries #slow query reporting mysql #small business infrastructure #small business IT #small business servers #small infrastructure monitoring #small server monitoring #small-business-security #small-business-tech #source-built-linux #ssh bastion #storage capacity planning linux #storage monitoring #subnets #sysadmin-checklist #sysadmin-lab #System Health #system health reporting #systemd #tcp-mtu-probing #tcp-tuning #terraform oci compute #terraform oracle cloud infrastructure #track-disk-growth-linux #Trend Monitoring #trend-analysis #trends #tuned-adm #Tutorial #uptime-checks #uptime-monitoring #vcn #vcn-design #vector #vsftpd #vulnerability-response #wazuh #weekly-server-report #windows-agent #xrdp