Skip to content

Instantly share code, notes, and snippets.

@DimmKirr
Last active July 23, 2025 04:52
Show Gist options
  • Save DimmKirr/ccb720501a1a8ec65033557d0ec4932b to your computer and use it in GitHub Desktop.
Save DimmKirr/ccb720501a1a8ec65033557d0ec4932b to your computer and use it in GitHub Desktop.
Schedule Command

Schedule any command

This small sh function allows to schedule a command to be executed at a specific time. I use it for scheduling Claude Code "Continue" text in my tmux session

asciicast

function schedule_command() {
# Example usage:
# 1. Schedule a command to run at 20:00 UTC in session "WORK" window 3, pane 1:
# schedule_command "WORK:3" 1 "20:00" "echo 'Daily backup starting'"
#
# 2. Schedule a database maintenance task at midnight UTC:
# schedule_command "DB:1" 0 "00:00" "pg_repack -d mydb -t large_table"
#
# 3. Schedule a reminder in the current session:
# schedule_command "$(tmux display-message -p '#{session_name}'):$(tmux display-message -p '#{window_index}')" "$(tmux display-message -p '#{pane_index}')" "15:30" "echo 'Time for the team meeting!'"
session_window="$1" # Format: "SESSION:WINDOW" (e.g., "KIRR:3")
window_index="$2" # The window index (e.g., 1, 2, 3, etc.)
target_time="$3" # Format: "HH:MM" in UTC (e.g., "20:00" or "00:00")
command="$4" # The command to execute
# Get the pane ID for the specified window index
pane_id=$(tmux list-panes -t "$session_window" | grep "^$window_index:" | awk '{print $1}' | sed 's/[^0-9%]//g')
if [ -z "$pane_id" ]; then
echo "Error: Could not find pane with index $window_index in $session_window"
return 1
fi
# Get current time components in UTC
current_hour=$(date -u +"%H")
current_minute=$(date -u +"%M")
current_second=$(date -u +"%S")
current_date=$(date -u +"%Y-%m-%d")
# Extract hours and minutes from target time
target_hour=${target_time%:*}
target_minute=${target_time#*:}
# Calculate current and target time in seconds since midnight
current_seconds_since_midnight=$(( 10#$current_hour * 3600 + 10#$current_minute * 60 + 10#$current_second ))
target_seconds_since_midnight=$(( 10#$target_hour * 3600 + 10#$target_minute * 60 ))
# Determine if the target time is today or tomorrow
if (( current_seconds_since_midnight >= target_seconds_since_midnight )); then
# If current time is past the target time, schedule for tomorrow
# On macOS, use -v for date adjustment
target_date=$(date -u -d "tomorrow" +"%Y-%m-%d")
delay=$(( 86400 - current_seconds_since_midnight + target_seconds_since_midnight ))
else
# Schedule for today
target_date="$current_date"
delay=$(( target_seconds_since_midnight - current_seconds_since_midnight ))
fi
# Sleep for the calculated delay in seconds
(sleep "$delay" && tmux send-keys -t "$pane_id" "$command" && tmux send-keys -t "$pane_id" ENTER) &
echo "Command scheduled to run at $target_time UTC on $target_date (in $delay seconds) on pane $pane_id (window index $window_index)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment