montana/Русский/Логистика/run_green_fleet.sh

51 lines
1.7 KiB
Bash

#!/bin/bash
# Wrapper that restarts mt_green_fleet.py on crash/watchdog exit.
# Reads checkpoint to pass --start N on restart.
# Usage: bash run_green_fleet.sh 2>&1 | tee /tmp/gf_wrapper.log
SCRIPT="C:/project/Cursor/Логистика/mt_green_fleet.py"
PYTHON="/c/Users/0/AppData/Local/Programs/Python/Python314/python"
CHECKPOINT="C:/project/Cursor/Логистика/mt_green_fleet_progress.json"
LOG="/tmp/gf_wrapper.log"
MAX_RESTARTS=500
DELAY_BETWEEN_RESTARTS=15
restart=0
start_page=1
while [ $restart -lt $MAX_RESTARTS ]; do
restart=$((restart + 1))
echo "[WRAPPER] === Start run #$restart from page $start_page ===" 2>&1
# Kill any competing Chrome/Python from previous run
wmic process where "name='chrome.exe'" call terminate >/dev/null 2>&1 || true
# Run the scraper
"$PYTHON" -u "$SCRIPT" --delay 2.0 --start "$start_page"
exit_code=$?
echo "[WRAPPER] Run #$restart exited with code $exit_code"
if [ $exit_code -eq 0 ]; then
echo "[WRAPPER] Normal completion! Done."
break
fi
# Read checkpoint for resume page
if [ -f "$CHECKPOINT" ]; then
# Parse page from JSON: {"page": N, ...}
start_page=$("$PYTHON" -c "import json; d=json.load(open('$CHECKPOINT')); print(d.get('page', 1))" 2>/dev/null)
if [ -z "$start_page" ] || [ "$start_page" -lt 1 ] 2>/dev/null; then
start_page=1
fi
echo "[WRAPPER] Checkpoint: resuming from page $start_page"
else
echo "[WRAPPER] No checkpoint, starting from page 1"
start_page=1
fi
echo "[WRAPPER] Sleeping $DELAY_BETWEEN_RESTARTS seconds before restart..."
sleep $DELAY_BETWEEN_RESTARTS
done
echo "[WRAPPER] Done after $restart runs."