Skip to content

Instantly share code, notes, and snippets.

@o-az
Last active November 14, 2025 13:55
Show Gist options
  • Select an option

  • Save o-az/c1051dbb729b2c507f82c5d99551b81b to your computer and use it in GitHub Desktop.

Select an option

Save o-az/c1051dbb729b2c507f82c5d99551b81b to your computer and use it in GitHub Desktop.
tell your agent to wrap any command it runs with `command.sh`
  • Critical: when running any command, always wrap it with ~/dev/commands/command.sh in the following format:

    /bin/bash ~/dev/commands/command.sh --timeout 30 <command>

    Examples:

    • /bin/bash ~/dev/commands/command.sh --timeout 30 wrangler dev
    • /bin/bash ~/dev/commands/command.sh --timeout 30 bun dev
    • /bin/bash ~/dev/commands/command.sh --timeout 30 pnpm --filter server start
    • and so on... This will ensure that the command will terminate automatically after 30 seconds if it doesn't complete. Without this, hours are wasted waiting and realizing that you ran a dev command and never was able to exit it and resume your work.
#!/usr/bin/env bash
set -eou pipefail
# Colors (works on macOS, Linux, and most terminals)
CYAN='\033[0;36m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RESET='\033[0m'
# Default timeout in seconds (30 seconds)
DEFAULT_TIMEOUT=30
# Parse arguments
TIMEOUT=$DEFAULT_TIMEOUT
COMMAND_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
--timeout|-t)
TIMEOUT="$2"
shift 2
;;
*)
COMMAND_ARGS+=("$1")
shift
;;
esac
done
# Check if command was provided
if [ ${#COMMAND_ARGS[@]} -eq 0 ]; then
echo "Usage: $0 [--timeout SECONDS] <command> [args...]"
echo "Default timeout: ${DEFAULT_TIMEOUT}s"
exit 1
fi
echo -e "${CYAN}Running: ${COMMAND_ARGS[*]} (timeout: ${TIMEOUT}s)${RESET}"
echo ""
# Run the command in the background
"${COMMAND_ARGS[@]}" &
CMD_PID=$!
# Monitor the command with timeout (using deciseconds for integer math)
ELAPSED=0
TIMEOUT_DS=$((TIMEOUT * 10)) # Convert to deciseconds
while [ $ELAPSED -lt $TIMEOUT_DS ]; do
# Check if command is still running
if ! kill -0 $CMD_PID 2>/dev/null; then
# Command finished
wait $CMD_PID 2>/dev/null || true
ELAPSED_SEC=$(echo "scale=1; $ELAPSED / 10" | bc)
echo ""
echo -e "${GREEN}Command completed in ${ELAPSED_SEC}s${RESET}"
echo -e "${CYAN}Finished running: ${COMMAND_ARGS[*]}${RESET}"
exit 0
fi
sleep 0.1
ELAPSED=$((ELAPSED + 1))
done
# If we get here, timeout was reached
echo ""
echo -e "${YELLOW}Command timed out after ${TIMEOUT}s - gracefully exiting${RESET}"
# Send SIGTERM to allow graceful shutdown
kill -TERM $CMD_PID 2>/dev/null || true
# Give it a moment to clean up
sleep 1
# Force kill if still running
if kill -0 $CMD_PID 2>/dev/null; then
kill -9 $CMD_PID 2>/dev/null || true
fi
# Wait to clean up zombie process
wait $CMD_PID 2>/dev/null || true
echo -e "${CYAN}Finished running: ${COMMAND_ARGS[*]}${RESET}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment