Compare commits
2 Commits
31a0cb1a9f
...
ef42a7089f
Author | SHA1 | Date | |
---|---|---|---|
ef42a7089f | |||
e46aba65be |
@@ -1,22 +1,28 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# tmux-daemonizer.sh
|
# tmux-daemon.sh
|
||||||
# Idempotent tmux daemon manager with optional per-command workdir
|
# Idempotent tmux daemon manager with config file support
|
||||||
|
|
||||||
|
# Load config file if it exists
|
||||||
|
SCRIPT_DIR=$(dirname "$0")
|
||||||
|
SCRIPT_NAME=$(basename "$0" .sh)
|
||||||
|
CONFIG_FILE="${SCRIPT_DIR}/${SCRIPT_NAME}.conf"
|
||||||
|
if [ -f "$CONFIG_FILE" ]; then
|
||||||
|
echo "Loading config from $CONFIG_FILE"
|
||||||
|
source "$CONFIG_FILE"
|
||||||
|
else
|
||||||
|
echo "Config file $CONFIG_FILE not found, generating template..."
|
||||||
|
cat > "$CONFIG_FILE" << 'EOF'
|
||||||
|
# tmux-daemon.conf
|
||||||
|
# Configuration file for tmux-daemon.sh
|
||||||
|
|
||||||
|
# Session name
|
||||||
SESSION="example"
|
SESSION="example"
|
||||||
echo "[main] SESSION: $SESSION"
|
|
||||||
|
|
||||||
|
# Whether to attach to session after setup (0 or 1)
|
||||||
ATTACH_SESSION=0
|
ATTACH_SESSION=0
|
||||||
|
|
||||||
while getopts "a" opt; do
|
# Commands to run (array format)
|
||||||
case "$opt" in
|
# Format: "workdir:::command" OR just "command"
|
||||||
a) ATTACH_SESSION=1 ;;
|
|
||||||
\?) echo "Usage: $0 [-a]" >&2
|
|
||||||
exit 1 ;;
|
|
||||||
esac
|
|
||||||
done
|
|
||||||
shift $((OPTIND-1))
|
|
||||||
|
|
||||||
# Define commands: "workdir:::command" OR just "command"
|
|
||||||
CMDS=(
|
CMDS=(
|
||||||
"ping google.com"
|
"ping google.com"
|
||||||
"ping google.com"
|
"ping google.com"
|
||||||
@@ -26,6 +32,23 @@ CMDS=(
|
|||||||
"ping google.com"
|
"ping google.com"
|
||||||
"ping google.com"
|
"ping google.com"
|
||||||
)
|
)
|
||||||
|
EOF
|
||||||
|
echo "Generated $CONFIG_FILE with default values. Please edit and run again."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Validate required variables
|
||||||
|
if [ -z "$SESSION" ]; then
|
||||||
|
echo "Error: SESSION must be set in $CONFIG_FILE" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ${#CMDS[@]} -eq 0 ]; then
|
||||||
|
echo "Error: CMDS array must be set in $CONFIG_FILE" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "[main] SESSION: $SESSION"
|
||||||
echo "[main] Commands: ${CMDS[*]}"
|
echo "[main] Commands: ${CMDS[*]}"
|
||||||
|
|
||||||
# Create session if missing
|
# Create session if missing
|
||||||
|
@@ -2,38 +2,51 @@
|
|||||||
# tmux-oneshot.sh
|
# tmux-oneshot.sh
|
||||||
# Run a single command in a tmux session
|
# Run a single command in a tmux session
|
||||||
|
|
||||||
# Hardcoded command and settings (uncomment and modify as needed)
|
# Load config file if it exists
|
||||||
# SESSION="mytest"
|
SCRIPT_DIR=$(dirname "$0")
|
||||||
# COMMAND="ping google.com"
|
SCRIPT_NAME=$(basename "$0" .sh)
|
||||||
# ATTACH_SESSION=1
|
CONFIG_FILE="${SCRIPT_DIR}/${SCRIPT_NAME}.conf"
|
||||||
|
if [ -f "$CONFIG_FILE" ]; then
|
||||||
SESSION=""
|
echo "Loading config from $CONFIG_FILE"
|
||||||
ATTACH_SESSION=0
|
source "$CONFIG_FILE"
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
# Use hardcoded command if no arguments provided
|
|
||||||
if [ $# -eq 0 ]; then
|
|
||||||
if [ -n "$COMMAND" ]; then
|
|
||||||
echo "Using hardcoded command: $COMMAND"
|
|
||||||
else
|
else
|
||||||
echo "Error: Command is required" >&2
|
echo "Config file $CONFIG_FILE not found, generating template..."
|
||||||
echo "Usage: $0 [-t <session_name>] [-a] <command>" >&2
|
cat > "$CONFIG_FILE" << 'EOF'
|
||||||
|
# tmux-oneshot.conf
|
||||||
|
# Configuration file for tmux-oneshot.sh
|
||||||
|
|
||||||
|
# Session name (leave empty to use first word of command)
|
||||||
|
SESSION=""
|
||||||
|
|
||||||
|
# Command to run (leave empty to use command line arguments)
|
||||||
|
COMMAND=""
|
||||||
|
|
||||||
|
# Whether to attach to session after running command (0 or 1)
|
||||||
|
ATTACH_SESSION=0
|
||||||
|
EOF
|
||||||
|
echo "Generated $CONFIG_FILE with default values. Please edit and run again."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Validate required variables
|
||||||
|
if [ -z "$SESSION" ] && [ -z "$COMMAND" ]; then
|
||||||
|
echo "Error: Either SESSION or COMMAND must be set in $CONFIG_FILE" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
else
|
|
||||||
|
# Use command line arguments if provided, otherwise use config
|
||||||
|
if [ $# -gt 0 ]; then
|
||||||
COMMAND="$*"
|
COMMAND="$*"
|
||||||
|
echo "Using command line arguments: $COMMAND"
|
||||||
|
else
|
||||||
|
if [ -z "$COMMAND" ]; then
|
||||||
|
echo "Error: No command provided and COMMAND not set in config" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "Using config command: $COMMAND"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Use hardcoded session name, or first word of command if not provided
|
# Use session name from config, or first word of command if not set
|
||||||
if [ -z "$SESSION" ]; then
|
if [ -z "$SESSION" ]; then
|
||||||
SESSION=$(echo "$COMMAND" | awk '{print $1}')
|
SESSION=$(echo "$COMMAND" | awk '{print $1}')
|
||||||
fi
|
fi
|
||||||
|
Reference in New Issue
Block a user