-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor_and_run.sh
More file actions
executable file
·100 lines (85 loc) · 2.25 KB
/
Copy pathmonitor_and_run.sh
File metadata and controls
executable file
·100 lines (85 loc) · 2.25 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/bin/bash
set -euo pipefail
# Configuration
readonly BOT_SCRIPT="ARES.py"
readonly LOG_FILE="ARES.log"
readonly VENV_DIR=".venv"
readonly BRANCH="main"
readonly CHECK_INTERVAL=60
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
}
install_uv() {
if ! command -v uv &> /dev/null; then
log "Installing uv..."
curl -LsSf https://astral.sh/uv/install.sh | sh
export PATH="$HOME/.cargo/bin:$PATH"
fi
}
setup_environment() {
if [ ! -d "$VENV_DIR" ]; then
log "Creating virtual environment..."
uv venv "$VENV_DIR"
fi
log "Installing dependencies..."
if [ -f "pyproject.toml" ]; then
uv sync
elif [ -f "requirements.txt" ]; then
uv pip install -r requirements.txt
fi
}
stop_bot() {
local pids
pids=$(pgrep -f "python.*$BOT_SCRIPT" || true)
if [ -n "$pids" ]; then
log "Stopping bot processes: $pids"
kill $pids 2>/dev/null || true
sleep 2
# Force kill if still running
pids=$(pgrep -f "python.*$BOT_SCRIPT" || true)
if [ -n "$pids" ]; then
kill -9 $pids 2>/dev/null || true
fi
fi
}
start_bot() {
stop_bot
log "Starting bot..."
nohup "$VENV_DIR/bin/python" "$BOT_SCRIPT" > "$LOG_FILE" 2>&1 &
log "Bot started with PID $!"
}
check_for_updates() {
if ! git fetch origin "$BRANCH" 2>&1 | grep -q "fatal"; then
local local_hash remote_hash
local_hash=$(git rev-parse HEAD)
remote_hash=$(git rev-parse "origin/$BRANCH")
if [ "$local_hash" != "$remote_hash" ]; then
log "Update detected: $local_hash -> $remote_hash"
git reset --hard "origin/$BRANCH" || {
log "ERROR: Failed to update repository"
return 1
}
return 0
fi
else
log "WARNING: Git fetch failed"
return 1
fi
return 2
}
# Initialize
log "Initializing bot monitor..."
install_uv
setup_environment
start_bot
# Main loop
while true; do
sleep "$CHECK_INTERVAL"
if check_for_updates; then
log "Updating dependencies..."
setup_environment
start_bot
fi
done