#!/bin/bash BASE_DIR="/home/shane/foodie_automator" LOG_DIR="$BASE_DIR/logs" LOCK_DIR="$BASE_DIR/locks" LOG_FILE="$LOG_DIR/manage_scripts.log" VENV_PYTHON="$BASE_DIR/venv/bin/python" CHECKSUM_FILE="$BASE_DIR/.file_checksum" mkdir -p "$LOG_DIR" "$LOCK_DIR" || { echo "Error: Failed to create directories"; exit 1; } log() { echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" echo "$1" } calculate_checksum() { find "$BASE_DIR" -type f \ -not -path "$BASE_DIR/logs/*" \ -not -path "$BASE_DIR/*.json" \ -not -path "$BASE_DIR/.file_checksum" \ -not -path "$BASE_DIR/venv/*" \ -not -path "$BASE_DIR/locks/*" \ -exec sha256sum {} \; | sort | sha256sum | awk '{print $1}' } check_running() { local script_name="$1" local lock_file="$LOCK_DIR/${script_name}.lock" if [ -f "$lock_file" ]; then local pid=$(cat "$lock_file") if ps -p "$pid" > /dev/null; then log "$script_name is already running (PID: $pid)" return 0 else log "Stale lock file for $script_name, removing" rm -f "$lock_file" fi fi return 1 } run_script() { local script="$1" local script_name="${script%.py}" local script_log="$LOG_DIR/${script_name}.log" if check_running "$script_name"; then echo "0" # Skip sleep return 1 fi log "Running $script..." "$VENV_PYTHON" "$BASE_DIR/$script" >> "$script_log" 2>&1 & local pid=$! echo "$pid" > "$LOCK_DIR/${script_name}.lock" wait "$pid" local exit_code=$? if [ $exit_code -eq 0 ]; then log "$script completed successfully" else log "$script failed with exit code $exit_code" fi sleep_time=$(grep "sleep_time:" "$script_log" | tail -n 1 | grep -oP 'sleep_time: \K[0-9]+' || echo $((RANDOM % 601 + 1200))) log "$script completed, sleep_time: $sleep_time seconds" rm -f "$LOCK_DIR/${script_name}.lock" echo "$sleep_time" } stop_scripts() { log "Stopping scripts..." for script in foodie_automator_rss.py foodie_automator_reddit.py foodie_automator_google.py; do if [ -f "$script" ]; then local script_name="${script%.py}" if pkill -TERM -f "$VENV_PYTHON.*$script_name"; then log "Sent TERM to $script_name" sleep 2 pkill -9 -f "$VENV_PYTHON.*$script_name" || true else log "No running $script_name found" fi rm -f "$LOCK_DIR/${script_name}.lock" log "Removed lock file for $script_name" fi done log "Scripts stopped." } update_dependencies() { log "Updating dependencies..." cd "$BASE_DIR" || { log "Failed to change to $BASE_DIR"; exit 1; } if [ ! -d "venv" ]; then python3 -m venv venv log "Created new virtual environment" fi source "$BASE_DIR/venv/bin/activate" log "Dependencies updated." } if [ "$1" == "stop" ]; then log "Received stop command, stopping all scripts..." stop_scripts for script in foodie_engagement_generator.py foodie_weekly_thread.py; do local script_name="${script%.py}" if pkill -TERM -f "$VENV_PYTHON.*$script_name"; then log "Sent TERM to $script_name" sleep 2 pkill -9 -f "$VENV_PYTHON.*$script_name" || true else log "No running $script_name found" fi rm -f "$LOCK_DIR/$script_name.lock" log "Stopped $script_name" done log "All scripts stopped. Reminder: Disable cron jobs (crontab -e)." exit 0 fi if [ "$1" == "start" ]; then log "Received start command, starting all scripts..." cd "$BASE_DIR" || { log "Failed to change to $BASE_DIR"; exit 1; } source "$BASE_DIR/venv/bin/activate" if [ -f "$BASE_DIR/.env" ]; then while IFS='=' read -r key value; do if [[ ! -z "$key" && ! "$key" =~ ^# ]]; then export "$key=$value" fi done < <(grep -v '^#' "$BASE_DIR/.env") log ".env variables loaded" else log "Error: .env file not found" exit 1 fi for script in foodie_automator_rss.py foodie_automator_reddit.py foodie_automator_google.py; do if [ -f "$script" ]; then sleep_time=$(run_script "$script" | tail -n 1) if [ "$sleep_time" != "0" ]; then log "Sleeping for $sleep_time seconds after $script" sleep "$sleep_time" fi else log "Script $script not found" fi done if [ -f "foodie_engagement_generator.py" ]; then if ! check_running "foodie_engagement_generator"; then log "Running foodie_engagement_generator.py..." "$VENV_PYTHON" "foodie_engagement_generator.py" >> "$LOG_DIR/foodie_engagement_generator.log" 2>&1 log "foodie_engagement_generator.py completed" fi fi log "All scripts started. Ensure cron jobs are enabled (crontab -l)." exit 0 fi 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" if pgrep -f "$VENV_PYTHON.*foodie_automator" > /dev/null; then stop_scripts fi update_dependencies echo "$CURRENT_CHECKSUM" > "$CHECKSUM_FILE" log "Checksum updated." fi cd "$BASE_DIR" source "$BASE_DIR/venv/bin/activate" if [ -f "$BASE_DIR/.env" ]; then while IFS='=' read -r key value; do if [[ ! -z "$key" && ! "$key" =~ ^# ]]; then export "$key=$value" fi done < <(grep -v '^#' "$BASE_DIR/.env") log ".env variables loaded" else log "Error: .env file not found" exit 1 fi for script in foodie_automator_rss.py foodie_automator_reddit.py foodie_automator_google.py; do if [ -f "$script" ]; then sleep_time=$(run_script "$script" | tail -n 1) if [ "$sleep_time" != "0" ]; then log "Sleeping for $sleep_time seconds after $script" sleep "$sleep_time" fi else log "Script $script not found" fi done log "All scripts processed." exit 0