diff --git a/manage_scripts.sh b/manage_scripts.sh new file mode 100755 index 0000000..559407a --- /dev/null +++ b/manage_scripts.sh @@ -0,0 +1,89 @@ +#!/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 + python3 foodie_automator_reddit.py & python3 foodie_automator_rss.py & python3 foodie_automator_google.py & + log "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 \ No newline at end of file