-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.sh
More file actions
69 lines (56 loc) · 2.46 KB
/
Copy pathrunner.sh
File metadata and controls
69 lines (56 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
# Ensure SCRIPT_PATH, LOG_PATH, and URL are defined before or above this block
# --- CONFIGURATION (SET THESE) ---
URL="https://<your_ngrok_url>" # CHANGE THIS
SCRIPT_PATH="/your/script/path.py"
LOG_PATH="/path/to/your/output/log_file.txt"
# --------------------------------
while true; do
# Targeted PID check (prevents script from matching/killing itself)
PID=$(pgrep -f "python3.*$SCRIPT_PATH" | head -n 1)
# 1. Check HTTP status with strict network timeouts
STATUS=$(curl -o /dev/null -s -w "%{http_code}" --connect-timeout 5 --max-time 10 "$URL/")
# Trigger restart if process is dead OR server returns non-200
if [ -z "$PID" ] || [ "$STATUS" != "200" ]; then
echo "[Monitor] Issue detected (PID: ${PID:-NONE}, HTTP: $STATUS). Restarting script..."
# Clean Kill Logic
if [ -n "$PID" ]; then
kill -9 "$PID" 2>/dev/null
sleep 1
if kill -0 "$PID" 2>/dev/null; then
pkill -9 -f "python3.*$SCRIPT_PATH"
sleep 1
fi
fi
# Spawn new process and capture its exact PID
nohup python3 "$SCRIPT_PATH" </dev/null >"$LOG_PATH" 2>&1 &
NEW_PID=$!
sleep 2
# --- RECOVERY & HEALTH CHECK LOOP ---
echo "[Monitor] Waiting for server to come back online..."
ATTEMPTS=0
MAX_ATTEMPTS=15 # Max ~30 seconds of waiting before forcing another restart
while true; do
# Safeguard 1: Check if the newly spawned process died immediately on boot
if ! kill -0 "$NEW_PID" 2>/dev/null; then
echo "[Monitor] ERROR: script crashed on startup! Retrying..."
break
fi
# Safeguard 2: Check status with network timeouts
STATUS_CHECK=$(curl -o /dev/null -s -w "%{http_code}" --connect-timeout 5 --max-time 10 "$URL/")
if [ "$STATUS_CHECK" = "200" ]; then
echo "[Monitor] Server is back online."
break # Recovery successful, return to main monitoring state
fi
# Safeguard 3: Break out of inner loop if HTTP stays down too long
ATTEMPTS=$((ATTEMPTS + 1))
if [ "$ATTEMPTS" -ge "$MAX_ATTEMPTS" ]; then
echo "[Monitor] Timeout waiting for 200 OK. Forcing fresh restart cycle..."
break
fi
sleep 2
done
fi
# Monitoring interval for normal operation
sleep 10
done