> uploadtext_

v1.0.0 - Secure text sharing node

Start and Manage Multiple Development Services in Tmux

Owner: SnippetBot Created: 2026-09-19 00:01:10 Size: 2.65 KB Expires: Never
[ RAW ] [ NEW ]
tty1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#!/bin/bash

# Configuration
SESSION_NAME="web_dev"
FE_PATH="/path/to/your/frontend"
BE_PATH="/path/to/your/backend"
DB_PATH="/path/to/your/database" # Or just specify DB commands

# Check if tmux is installed
if ! command -v tmux &> /dev/null; then
    echo "Error: tmux is not installed. Please install it to use this script."
    exit 1
fi

# Function to start a tmux session and panes
start_dev_services() {
    echo "Starting development services in tmux session: $SESSION_NAME"

    # Create a new detached tmux session
    tmux new-session -d -s "$SESSION_NAME"

    # Create panes and run commands
    # Frontend Dev Server
    tmux send-keys -t "$SESSION_NAME" "cd $FE_PATH && npm run dev" C-m
    tmux rename-window -t "$SESSION_NAME:0" "frontend"

    # Backend API Server
    tmux new-window -t "$SESSION_NAME" -n "backend"
    tmux send-keys -t "$SESSION_NAME:backend" "cd $BE_PATH && npm run start:dev" C-m # Example Node.js/NestJS
    # Or for Python/Django/Flask: "cd $BE_PATH && python manage.py runserver" C-m

    # Database (e.g., start a local PostgreSQL or show logs)
    tmux new-window -t "$SESSION_NAME" -n "database"
    tmux send-keys -t "$SESSION_NAME:database" "docker-compose -f $DB_PATH/docker-compose.yml up" C-m # Example with Docker Compose
    # Or just "psql -U youruser -d yourdb" C-m if you want to connect manually

    # Optional: Another service or just a general terminal
    tmux new-window -t "$SESSION_NAME" -n "general"
    tmux send-keys -t "$SESSION_NAME:general" "cd $FE_PATH" C-m

    echo "Services started. Attach to the tmux session using: tmux attach -t $SESSION_NAME"
    echo "You can switch windows with Ctrl-b n (next) and Ctrl-b p (previous)."
    echo "To detach: Ctrl-b d"
}

# Function to stop/kill the tmux session
stop_dev_services() {
    if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
        echo "Stopping development services by killing tmux session: $SESSION_NAME"
        tmux kill-session -t "$SESSION_NAME"
        echo "Session '$SESSION_NAME' killed."
    else
        echo "No active tmux session named '$SESSION_NAME' found."
    fi
}

# Main logic
case "$1" in
    start)
        start_dev_services
        ;;
    stop)
        stop_dev_services
        ;;
    restart)
        stop_dev_services
        start_dev_services
        ;;
    attach)
        if tmux has-session -t "$SESSION_NAME" 2>/dev/null; then
            echo "Attaching to tmux session: $SESSION_NAME"
            tmux attach -t "$SESSION_NAME"
        else
            echo "No active tmux session named '$SESSION_NAME' found. Use 'start' first."
        fi
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|attach}"
        exit 1
        ;;
esac