diff --git a/watch.sh b/watch.sh new file mode 100644 index 0000000..46a8f1a --- /dev/null +++ b/watch.sh @@ -0,0 +1,58 @@ +#!/bin/bash + +# Function to get the current git commit hash +get_git_hash() { + git rev-parse HEAD 2>/dev/null +} + +# Function to start the application +start_app() { + git pull + source bin/activate + pip install -r requirements.txt + python init.py & + APP_PID=$! + echo "Application started with PID: $APP_PID" +} + +# Function to stop the application +stop_app() { + if [ -n "$APP_PID" ] && kill -0 "$APP_PID" 2>/dev/null; then + echo "Stopping application (PID: $APP_PID)..." + kill "$APP_PID" 2>/dev/null + wait "$APP_PID" 2>/dev/null + fi +} + +# Cleanup on script exit +cleanup() { + echo "Shutting down..." + stop_app + exit 0 +} + +trap cleanup SIGINT SIGTERM + +# Initial start +CURRENT_HASH=$(get_git_hash) +start_app + +# Watch for changes +CHECK_INTERVAL=10 # seconds between checks + +while true; do + sleep $CHECK_INTERVAL + + # Fetch updates from remote + git fetch origin 2>/dev/null + + # Get the remote hash + REMOTE_HASH=$(git rev-parse origin/$(git rev-parse --abbrev-ref HEAD) 2>/dev/null) + + if [ "$CURRENT_HASH" != "$REMOTE_HASH" ]; then + echo "Git changes detected! Restarting..." + stop_app + CURRENT_HASH=$REMOTE_HASH + start_app + fi +done