Add a tmux script for running A single command once
This commit is contained in:
46
tmux-oneshot.sh
Normal file
46
tmux-oneshot.sh
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# tmux-oneshot.sh
|
||||||
|
# Run a single command in a tmux session
|
||||||
|
|
||||||
|
SESSION=""
|
||||||
|
ATTACH_SESSION=0
|
||||||
|
|
||||||
|
while getopts "t:a" opt; do
|
||||||
|
case "$opt" in
|
||||||
|
t) SESSION="$OPTARG" ;;
|
||||||
|
a) ATTACH_SESSION=1 ;;
|
||||||
|
\?) echo "Usage: $0 [-t <session_name>] [-a] <command>" >&2
|
||||||
|
exit 1 ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
shift $((OPTIND-1))
|
||||||
|
|
||||||
|
if [ $# -eq 0 ]; then
|
||||||
|
echo "Error: Command is required" >&2
|
||||||
|
echo "Usage: $0 [-t <session_name>] [-a] <command>" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
COMMAND="$*"
|
||||||
|
|
||||||
|
# Use first word of command as session name if -t not provided
|
||||||
|
if [ -z "$SESSION" ]; then
|
||||||
|
SESSION=$(echo "$COMMAND" | awk '{print $1}')
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create session if missing
|
||||||
|
if ! tmux has-session -t $SESSION 2>/dev/null; then
|
||||||
|
echo "Creating tmux session: $SESSION"
|
||||||
|
tmux new-session -d -s $SESSION
|
||||||
|
else
|
||||||
|
echo "Session $SESSION exists, reusing..."
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Send command to session
|
||||||
|
echo "Running command: $COMMAND"
|
||||||
|
tmux send-keys -t $SESSION "$COMMAND" C-m
|
||||||
|
|
||||||
|
if [ $ATTACH_SESSION -eq 1 ]; then
|
||||||
|
echo "Attaching to tmux session: $SESSION"
|
||||||
|
tmux attach -t $SESSION
|
||||||
|
fi
|
Reference in New Issue
Block a user