Skip to content

Instantly share code, notes, and snippets.

@vrdhn
Created July 7, 2022 12:54
Show Gist options
  • Save vrdhn/a2123a1a6208d6af3e59bd235a8ce72b to your computer and use it in GitHub Desktop.
Save vrdhn/a2123a1a6208d6af3e59bd235a8ce72b to your computer and use it in GitHub Desktop.
ensure a named windows on tmux exists, and run commands in it.
#!/bin/sh
## tmux-run <window-name> <cmd args ...>
## start if session not running
if [ "x$TMUX" = "x" ] ;
then
tmux new -As0 \; detach-client
fi
ID=$(tmux list-windows -F '#I #W' | grep -w $1 | cut -d ' ' -f1)
if [ "x$ID" = "x" ] ;
then
tmux new-window -n $1
ID=$(tmux list-windows -F '#I #W' | grep -w $1 | cut -d ' ' -f1)
fi
#tmux select-window -t $ID
shift
## Send ^C , to break the process, and make sure shell is active.
sleep 2
tmux send-keys -t $ID C-c
## send ^C again after a second . let's be sure we kill it.
sleep 1
tmux send-keys -t $ID C-c
for c in "$@"
do
tmux send-keys -t $ID -- "$c"
tmux send-keys -t $ID ' '
done
tmux send-keys -t $ID C-m
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment