Skip to content

Instantly share code, notes, and snippets.

@pda
Created December 14, 2021 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pda/b78b4e508c64bc7d14ecc90fb6c2286d to your computer and use it in GitHub Desktop.
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 πŸ‰
#!/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}}"
@pda
Copy link
Author

pda commented Dec 14, 2021

$ bin/goreman-tmux
tmux window 4: goreman (10 panes)
  βœ” 0: frontend
  βœ” 1: nginx
  βœ” 2: puma
  βœ” 3: relay
  βœ” 4: s3
  βœ” 5: sidekiq
  βœ” 6: site
  βœ” 7: smokescreen
  βœ” 8: webpacker
    9: zsh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment