Created
December 14, 2021 09:34
-
-
Save pda/b78b4e508c64bc7d14ecc90fb6c2286d to your computer and use it in GitHub Desktop.
Idempotently launches each Procfile service as a tmux pane in a background window. Cursed dense bash hacks π
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e -o pipefail -u | |
# Launches each Procfile service as a tmux pane in a background window. | |
# The window is created if it doesn't already exist. | |
# Panes are created on demand, and existing panes are restarted if stopped. | |
WINDOW_NAME="goreman" | |
tmux new-window -adS -n $WINDOW_NAME | |
tmux set -w -t $WINDOW_NAME monitor-activity off | |
tmux set -w -t $WINDOW_NAME pane-border-format " #{pane_index}: #{pane_title} " | |
tmux set -w -t $WINDOW_NAME pane-border-status top | |
wait="" | |
for p in $(goreman check | ruby -ne 'puts $&.split(/,\s*/) if /(?<=\().*(?=\))/'); do | |
status=$(tmux list-panes -t $WINDOW_NAME -f "#{==:#{pane_title},$p}" -F "#D #{?#{==:#{pane_current_command},$WINDOW_NAME},alive,dead}") | |
if [[ -z $status ]]; then | |
echo "$p starting" | |
pane=$(tmux split-window -bd -t $WINDOW_NAME -l 4 -PF '#D') | |
tmux select-pane -t "$pane" -T "$p" | |
sleep 0.1 # give shell a moment to start, for cleaner output | |
tmux send-keys -t "$pane" "goreman start $p" "C-m" | |
wait="1" | |
elif [[ $status =~ dead ]]; then | |
echo "$p restarting" | |
tmux send-keys -t "${status%% *}" "goreman start $p" "C-m" | |
wait="1" | |
fi | |
done | |
tmux select-layout -t $WINDOW_NAME tiled | |
# Wait a moment for (re)started processes before listing panes. | |
if [[ $wait == "1" ]]; then sleep 1; fi | |
tmux list-windows -f "#{==:#{window_name},$WINDOW_NAME}" \ | |
-F 'tmux window #{window_index}: #{window_name} (#{window_panes} panes)' | |
tmux list-panes -t $WINDOW_NAME \ | |
-F " #{?#{==:#{pane_current_command},$WINDOW_NAME},β,#{?#{!=:#{pane_title},#H},β, }} #P: #{?#{==:#{pane_title},#H},#{pane_current_command},#{pane_title}}" |
Author
pda
commented
Dec 14, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment