|
|
|
|
@ -1,5 +1,87 @@ |
|
|
|
|
#!/bin/bash |
|
|
|
|
|
|
|
|
|
# Ensure logs and locks directories exist |
|
|
|
|
BASE_DIR="/home/shane/foodie_automator" |
|
|
|
|
LOG_DIR="$BASE_DIR/logs" |
|
|
|
|
LOCK_DIR="$BASE_DIR/locks" |
|
|
|
|
mkdir -p "$LOG_DIR" "$LOCK_DIR" || { echo "Error: Failed to create $LOG_DIR or $LOCK_DIR"; exit 1; } |
|
|
|
|
|
|
|
|
|
# Handle stop command |
|
|
|
|
if [ "$1" == "stop" ]; then |
|
|
|
|
log "Received stop command, stopping all scripts..." |
|
|
|
|
stop_scripts # Stop foodie_automator_*.py scripts (includes lock file removal) |
|
|
|
|
# Stop foodie_engagement_generator.py |
|
|
|
|
if pkill -TERM -f "$VENV_PYTHON.*foodie_engagement_generator"; then |
|
|
|
|
log "Sent TERM to foodie_engagement_generator.py" |
|
|
|
|
sleep 2 |
|
|
|
|
pkill -9 -f "$VENV_PYTHON.*foodie_engagement_generator" || true |
|
|
|
|
else |
|
|
|
|
log "No running foodie_engagement_generator.py found" |
|
|
|
|
fi |
|
|
|
|
rm -f "$LOCK_DIR/foodie_engagement_generator.lock" || log "Failed to remove foodie_engagement_generator.lock" |
|
|
|
|
log "Stopped foodie_engagement_generator.py" |
|
|
|
|
# Stop foodie_weekly_thread.py |
|
|
|
|
if pkill -TERM -f "$VENV_PYTHON.*foodie_weekly_thread"; then |
|
|
|
|
log "Sent TERM to foodie_weekly_thread.py" |
|
|
|
|
sleep 2 |
|
|
|
|
pkill -9 -f "$VENV_PYTHON.*foodie_weekly_thread" || true |
|
|
|
|
else |
|
|
|
|
log "No running foodie_weekly_thread.py found" |
|
|
|
|
fi |
|
|
|
|
rm -f "$LOCK_DIR/foodie_weekly_thread.lock" || log "Failed to remove foodie_weekly_thread.lock" |
|
|
|
|
log "Stopped foodie_weekly_thread.py" |
|
|
|
|
log "All scripts stopped. Reminder: Disable cron jobs to prevent automatic restarts (crontab -e)." |
|
|
|
|
exit 0 |
|
|
|
|
fi |
|
|
|
|
|
|
|
|
|
# Handle start command |
|
|
|
|
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" || { log "Failed to activate virtual environment"; exit 1; } |
|
|
|
|
if [ -f "$BASE_DIR/.env" ]; then |
|
|
|
|
export $(grep -v '^#' "$BASE_DIR/.env" | xargs) |
|
|
|
|
log ".env variables loaded" |
|
|
|
|
else |
|
|
|
|
log "Error: .env file not found at $BASE_DIR/.env" |
|
|
|
|
exit 1 |
|
|
|
|
fi |
|
|
|
|
# Start foodie_automator scripts with random 20-30 minute delays |
|
|
|
|
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") |
|
|
|
|
if [ -n "$sleep_time" ]; then |
|
|
|
|
log "Sleeping for $sleep_time seconds after $script" |
|
|
|
|
sleep "$sleep_time" |
|
|
|
|
fi |
|
|
|
|
else |
|
|
|
|
log "Script $script not found" |
|
|
|
|
fi |
|
|
|
|
done |
|
|
|
|
# Start foodie_engagement_generator.py |
|
|
|
|
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" >> "$BASE_DIR/logs/foodie_engagement_generator.log" 2>&1 |
|
|
|
|
log "foodie_engagement_generator.py completed" |
|
|
|
|
fi |
|
|
|
|
else |
|
|
|
|
log "Script foodie_engagement_generator.py not found" |
|
|
|
|
fi |
|
|
|
|
# Start foodie_weekly_thread.py |
|
|
|
|
if [ -f "foodie_weekly_thread.py" ]; then |
|
|
|
|
if ! check_running "foodie_weekly_thread"; then |
|
|
|
|
log "Running foodie_weekly_thread.py..." |
|
|
|
|
"$VENV_PYTHON" "foodie_weekly_thread.py" >> "$BASE_DIR/logs/foodie_weekly_thread.log" 2>&1 |
|
|
|
|
log "foodie_weekly_thread.py completed" |
|
|
|
|
fi |
|
|
|
|
else |
|
|
|
|
log "Script foodie_weekly_thread.py not found" |
|
|
|
|
fi |
|
|
|
|
log "All scripts started. Ensure cron jobs are enabled for automatic scheduling (crontab -l)." |
|
|
|
|
exit 0 |
|
|
|
|
fi |
|
|
|
|
|
|
|
|
|
# Directory to monitor |
|
|
|
|
BASE_DIR="/home/shane/foodie_automator" |
|
|
|
|
CHECKSUM_FILE="$BASE_DIR/.file_checksum" |
|
|
|
|
@ -51,7 +133,7 @@ run_script() { |
|
|
|
|
log "Running $script..." |
|
|
|
|
# Run script and capture output |
|
|
|
|
"$VENV_PYTHON" "$script" >> "$script_log" 2>&1 |
|
|
|
|
# Extract sleep_time from the last log line |
|
|
|
|
# Extract sleep_time from the last log line or generate random 20-30 minutes |
|
|
|
|
sleep_time=$(tail -n 1 "$script_log" | grep -oP 'sleep_time: \K[0-9]+' || echo $((RANDOM % 601 + 1200))) |
|
|
|
|
log "$script completed, sleep_time: $sleep_time seconds" |
|
|
|
|
echo "$sleep_time" |
|
|
|
|
|