Skip to content

Instantly share code, notes, and snippets.

@neonexus
Last active June 2, 2023 03:32
Show Gist options
  • Save neonexus/22225a53eb5ebd96c69d to your computer and use it in GitHub Desktop.
Save neonexus/22225a53eb5ebd96c69d to your computer and use it in GitHub Desktop.
Start / stop / restart scripts in TMUX
#!/bin/bash
#if [[ $EUID -ne 0 ]]; then
# echo "Script MUST be run as a root user"
# exit 1
#fi
session="bg-apps"
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."
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" "bash"
fi
function run_in_window {
name="$1"
cmd="$2"
if window_is_open "$name"; then
echo "Already running: $name"
else
echo "Starting: $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"
# 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"
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_is_open {
name="$1"
if (tmux list-windows -t "$session" -F '#{window_name}' | grep "^$name\$" > /dev/null); then
return 0
else
return 1
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
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="myapp"
else
cmd="$1"
fi
case "$2" in
status)
if window_is_open "$cmd"; then
echo "$cmd appears to be running"
else
echo "$cmd is not running"
fi
;;
stop)
stop_something "$cmd"
;;
restart)
stop_something "$cmd"
i=0
while window_is_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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment