#!/bin/bash # Directory to monitor BASE_DIR="/home/shane/foodie_automator" CHECKSUM_FILE="$BASE_DIR/.file_checksum" LOG_FILE="$BASE_DIR/manage_scripts.log" # Log function log() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" } # Calculate checksum of files (excluding logs, JSON files, and venv) calculate_checksum() { find "$BASE_DIR" -type f \ -not -path "$BASE_DIR/*.log" \ -not -path "$BASE_DIR/*.json" \ -not -path "$BASE_DIR/.file_checksum" \ -not -path "$BASE_DIR/venv/*" \ -exec sha256sum {} \; | sort | sha256sum | awk '{print $1}' } # Check if scripts are running check_running() { pgrep -f "python3.*foodie_automator" > /dev/null } # Stop scripts stop_scripts() { log "Stopping scripts..." pkill -TERM -f "python3.*foodie_automator" || true sleep 10 pkill -9 -f "python3.*foodie_automator" || true log "Scripts stopped." } # Start scripts start_scripts() { log "Starting scripts..." cd "$BASE_DIR" source venv/bin/activate # Find all foodie_automator_*.py scripts and start them for script in foodie_automator_*.py; do if [ -f "$script" ]; then log "Starting $script..." nohup python3 "$script" >> "${script%.py}.log" 2>&1 & fi done log "All scripts started." } # Update dependencies update_dependencies() { log "Updating dependencies..." cd "$BASE_DIR" # Create venv if it doesn't exist if [ ! -d "venv" ]; then python3 -m venv venv fi source venv/bin/activate pip install --upgrade pip pip install -r requirements.txt || (pip install requests openai beautifulsoup4 feedparser praw duckduckgo_search selenium Pillow pytesseract webdriver-manager && log "Fallback: Installed core dependencies") log "Dependencies updated." } # Main logic log "Checking for file changes..." CURRENT_CHECKSUM=$(calculate_checksum) if [ -f "$CHECKSUM_FILE" ]; then PREVIOUS_CHECKSUM=$(cat "$CHECKSUM_FILE") else PREVIOUS_CHECKSUM="" fi if [ "$CURRENT_CHECKSUM" != "$PREVIOUS_CHECKSUM" ]; then log "File changes detected. Previous checksum: $PREVIOUS_CHECKSUM, Current checksum: $CURRENT_CHECKSUM" # Stop scripts if running if check_running; then stop_scripts fi # Update dependencies update_dependencies # Start scripts start_scripts # Update checksum echo "$CURRENT_CHECKSUM" > "$CHECKSUM_FILE" log "Checksum updated." else log "No file changes detected." fi