Skip to content

Instantly share code, notes, and snippets.

@neonexus
Last active June 2, 2023 03:32

Revisions

  1. neonexus revised this gist Jun 2, 2023. No changes.
  2. neonexus renamed this gist Jun 2, 2023. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. neonexus revised this gist May 11, 2019. 1 changed file with 12 additions and 6 deletions.
    18 changes: 12 additions & 6 deletions Start scripts in TMUX.sh
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ fi
    function run_in_window {
    name="$1"
    cmd="$2"
    if window_still_open "$name"; then
    if window_is_open "$name"; then
    echo "Already running: $name"
    else
    echo "Starting: $name"
    @@ -48,15 +48,15 @@ function run_in_window {
    function stop_something {
    name="$1"

    if window_still_open "$name"; then
    if window_is_open "$name"; then
    echo "Stopping: $name"
    tmux send-keys -t "$session:$name" C-c
    else
    echo "Not running: $name"
    fi
    }

    function window_still_open {
    function window_is_open {
    name="$1"

    if (tmux list-windows -t "$session" -F '#{window_name}' | grep "^$name\$" > /dev/null); then
    @@ -66,23 +66,29 @@ function window_still_open {
    fi
    }

    ##################################
    # This is the function to change #
    ##################################
    function start_something {
    name="$1"

    case "$name" in
    # define the different types of commands to trigger inside of the TMUX session here

    launch)
    myapp)
    run_in_window "$name" "echo 'change this echo statement'"
    ;;
    *)
    echo "Unrecognized command: $1"
    esac
    }
    ##################################
    # END
    ##################################

    if [ "$1" = "" ]; then
    # set the default command to run here (name from case in start_something function)
    cmd="launch"
    cmd="myapp"
    else
    cmd="$1"
    fi
    @@ -102,7 +108,7 @@ case "$2" in
    stop_something "$cmd"

    i=0
    while window_still_open "$cmd"; do
    while window_is_open "$cmd"; do
    if [ $i -gt 10 ]; then
    echo "Taking too long, force-quiting"
    tmux kill-window -t "$session:$cmd"
  4. neonexus revised this gist Jul 10, 2015. 1 changed file with 38 additions and 36 deletions.
    74 changes: 38 additions & 36 deletions Start scripts in TMUX.sh
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,15 @@
    #!/bin/bash

    if [[ $EUID -ne 0 ]]; then
    echo "Script MUST be run as a root user"
    exit 1
    fi

    #if [[ $EUID -ne 0 ]]; then
    # echo "Script MUST be run as a root user"
    # exit 1
    #fi
    session="bg-apps"
    logs_dir=/opt/tmux-logs

    logs_dir=~/tmux-logs
    mkdir -p "$logs_dir"

    # Start session if it doesn't exist
    if (tmux has-session -t "$session" 2> /dev/null); then
    echo "Session $session exists."
    @@ -23,26 +23,28 @@ else
    echo "Error: cannot start session from within tmux."
    exit 1
    fi
    tmux new-session -d -s "$session"
    tmux new-session -d -s "$session" "bash"
    fi

    function run_in_window {
    name="$1"
    cmd="$2"
    if window_still_open "$name"; then
    echo "Already running: $name"
    else
    echo "Starting: $name"
    echo "Started at $(date)" > $logs_dir/$name
    echo "Command: $cmd" >> $logs_dir/$name
    echo "-------" >> $logs_dir/$name
    echo "Started at $(date)" > $logs_dir/$name.log
    echo "Command: $cmd" >> $logs_dir/$name.log
    echo "-------" >> $logs_dir/$name.log
    # Create new window and run command.
    tmux new-window -t "$session" -n "$name" "$cmd"
    # Start logging.
    tmux pipe-pane -t "$session:$name" "cat >> \"$logs_dir/$name\""
    tmux new-window -t "$session" -n "$name"
    # Check if in BASH, if not, start BASH
    # When this script is run via cron, it doesn't initiate BASH, this fixes that
    tmux send-keys -t "$session:$name" "if [ -z \$BASH ]; then bash; fi" C-m
    tmux send-keys -t "$session:$name" "$cmd" C-m
    fi
    }

    function stop_something {
    name="$1"

    @@ -53,21 +55,7 @@ function stop_something {
    echo "Not running: $name"
    fi
    }

    function start_something {
    name="$1"

    case "$name" in
    # define the different types of commands to trigger inside of the TMUX session here

    logstash)
    run_in_window "$name" "/opt/logstash/bin/logstash -f /opt/logstash/conf"
    ;;
    *)
    echo "Unrecognized command: $1"
    esac
    }


    function window_still_open {
    name="$1"

    @@ -78,13 +66,27 @@ function window_still_open {
    fi
    }

    function start_something {
    name="$1"

    case "$name" in
    # define the different types of commands to trigger inside of the TMUX session here

    launch)
    run_in_window "$name" "echo 'change this echo statement'"
    ;;
    *)
    echo "Unrecognized command: $1"
    esac
    }

    if [ "$1" = "" ]; then
    # set the default command to run here (name from case in start_something)
    cmd="logstash"
    # set the default command to run here (name from case in start_something function)
    cmd="launch"
    else
    cmd="$1"
    fi

    case "$2" in
    status)
    if window_is_open "$cmd"; then
  5. neonexus revised this gist Jul 16, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Start scripts in TMUX.sh
    Original file line number Diff line number Diff line change
    @@ -87,7 +87,7 @@ fi

    case "$2" in
    status)
    if window_is_open "$cmd"; do
    if window_is_open "$cmd"; then
    echo "$cmd appears to be running"
    else
    echo "$cmd is not running"
  6. neonexus revised this gist Jul 16, 2014. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions Start scripts in TMUX.sh
    Original file line number Diff line number Diff line change
    @@ -86,6 +86,13 @@ else
    fi

    case "$2" in
    status)
    if window_is_open "$cmd"; do
    echo "$cmd appears to be running"
    else
    echo "$cmd is not running"
    fi
    ;;
    stop)
    stop_something "$cmd"
    ;;
  7. neonexus revised this gist Jul 16, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Start scripts in TMUX.sh
    Original file line number Diff line number Diff line change
    @@ -94,7 +94,7 @@ case "$2" in

    i=0
    while window_still_open "$cmd"; do
    if [ i -gt 10 ]; then
    if [ $i -gt 10 ]; then
    echo "Taking too long, force-quiting"
    tmux kill-window -t "$session:$cmd"
    break
  8. neonexus renamed this gist Jul 16, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  9. neonexus created this gist Jul 16, 2014.
    112 changes: 112 additions & 0 deletions Start scripts in TMUX
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,112 @@
    #!/bin/bash

    if [[ $EUID -ne 0 ]]; then
    echo "Script MUST be run as a root user"
    exit 1
    fi

    session="bg-apps"
    logs_dir=/opt/tmux-logs

    mkdir -p "$logs_dir"

    # Start session if it doesn't exist
    if (tmux has-session -t "$session" 2> /dev/null); then
    echo "Session $session exists."
    else
    echo "Starting session $session."
    # tmux doesn't allow nested sessions, and if this situation is not caught here then the
    # 'tmux new-session' command will fail. Although, since the session is being started
    # detached, it is probably okay. So maybe it would be better to just temporarily unset the
    # TMUX variable before running new-session.
    if [ "$TMUX" != "" ]; then
    echo "Error: cannot start session from within tmux."
    exit 1
    fi
    tmux new-session -d -s "$session"
    fi

    function run_in_window {
    name="$1"
    cmd="$2"
    if window_still_open "$name"; then
    echo "Already running: $name"
    else
    echo "Starting: $name"
    echo "Started at $(date)" > $logs_dir/$name
    echo "Command: $cmd" >> $logs_dir/$name
    echo "-------" >> $logs_dir/$name
    # Create new window and run command.
    tmux new-window -t "$session" -n "$name" "$cmd"
    # Start logging.
    tmux pipe-pane -t "$session:$name" "cat >> \"$logs_dir/$name\""
    fi
    }

    function stop_something {
    name="$1"

    if window_still_open "$name"; then
    echo "Stopping: $name"
    tmux send-keys -t "$session:$name" C-c
    else
    echo "Not running: $name"
    fi
    }

    function start_something {
    name="$1"

    case "$name" in
    # define the different types of commands to trigger inside of the TMUX session here

    logstash)
    run_in_window "$name" "/opt/logstash/bin/logstash -f /opt/logstash/conf"
    ;;
    *)
    echo "Unrecognized command: $1"
    esac
    }

    function window_still_open {
    name="$1"

    if (tmux list-windows -t "$session" -F '#{window_name}' | grep "^$name\$" > /dev/null); then
    return 0
    else
    return 1
    fi
    }

    if [ "$1" = "" ]; then
    # set the default command to run here (name from case in start_something)
    cmd="logstash"
    else
    cmd="$1"
    fi

    case "$2" in
    stop)
    stop_something "$cmd"
    ;;
    restart)
    stop_something "$cmd"

    i=0
    while window_still_open "$cmd"; do
    if [ i -gt 10 ]; then
    echo "Taking too long, force-quiting"
    tmux kill-window -t "$session:$cmd"
    break
    else
    echo "Window still open, waiting..."
    sleep 2
    ((i++))
    fi
    done

    start_something "$cmd"
    ;;
    *)
    start_something "$cmd"
    esac