Skip to content

Commit ffd60f7

Browse files
committed
fix: simplify build-and-restart-telegram.sh, drop dead lock/PID file
- Remove unused LOCK_FILE and PID_FILE (odek has its own singleton lock) - Remove ODEK_TELEGRAM_LOG_FILE env (odek uses config.json for log path) - cd to PROJECT_DIR before starting bot so cwd is correct - Replace nohup with exec for cleaner process management
1 parent 9ba4d04 commit ffd60f7

1 file changed

Lines changed: 95 additions & 0 deletions

File tree

build-and-restart-telegram.sh

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/env bash
2+
# build-and-restart-telegram.sh — Build odek and restart the Telegram bot.
3+
#
4+
# Usage:
5+
# ./build-and-restart-telegram.sh # build + restart
6+
# ./build-and-restart-telegram.sh --build-only # build only
7+
# ./build-and-restart-telegram.sh --restart-only # restart only
8+
#
9+
# The bot binary is installed to /usr/local/bin/odek.
10+
# odek's own singleton lock (~/.odek/telegram.pid) prevents duplicates.
11+
12+
set -euo pipefail
13+
14+
PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
15+
BINARY="/usr/local/bin/odek"
16+
STDERR_LOG="/tmp/odek-telegram.log"
17+
18+
BUILD=true
19+
RESTART=true
20+
21+
# ── Parse flags ──────────────────────────────────────────────────────────
22+
for arg in "$@"; do
23+
case "$arg" in
24+
--build-only) RESTART=false ;;
25+
--restart-only) BUILD=false ;;
26+
*) echo "Unknown flag: $arg"; exit 1 ;;
27+
esac
28+
done
29+
30+
# ── Build ────────────────────────────────────────────────────────────────
31+
if $BUILD; then
32+
echo "🔨 Building odek..."
33+
cd "$PROJECT_DIR"
34+
go build -o "$BINARY" ./cmd/odek/
35+
echo " ✓ Binary: $BINARY ($(du -h "$BINARY" | cut -f1))"
36+
fi
37+
38+
# ── Restart ──────────────────────────────────────────────────────────────
39+
if ! $RESTART; then
40+
echo " (--build-only: skipping restart)"
41+
exit 0
42+
fi
43+
44+
echo ""
45+
echo "🔄 Restarting odek telegram bot..."
46+
47+
# Kill any running odek telegram process.
48+
KILLED=false
49+
while IFS= read -r pid; do
50+
if [[ -n "$pid" ]] && kill -0 "$pid" 2>/dev/null; then
51+
echo " Killing odek telegram (PID $pid)..."
52+
kill "$pid" 2>/dev/null || true
53+
KILLED=true
54+
fi
55+
done < <(pgrep -f "odek telegram" 2>/dev/null || true)
56+
57+
# Wait for graceful shutdown (odek's singleton lock handles cleanup).
58+
if $KILLED; then
59+
echo " Waiting for graceful shutdown..."
60+
for i in $(seq 1 30); do
61+
if ! pgrep -f "odek telegram" >/dev/null 2>&1; then
62+
echo " ✓ Old process exited after ${i}s"
63+
break
64+
fi
65+
sleep 1
66+
done
67+
68+
# Force kill if still alive.
69+
if pgrep -f "odek telegram" >/dev/null 2>&1; then
70+
echo " ⚠ Force-killing stubborn process..."
71+
pkill -9 -f "odek telegram" 2>/dev/null || true
72+
sleep 1
73+
fi
74+
else
75+
echo " No existing odek telegram process found."
76+
fi
77+
78+
# Start the bot from the project directory so cwd is correct.
79+
echo " Starting odek telegram..."
80+
cd "$PROJECT_DIR"
81+
exec "$BINARY" telegram 2>"$STDERR_LOG" &
82+
83+
new_pid=$!
84+
echo " ✓ odek telegram started (PID $new_pid)"
85+
echo " Stderr: $STDERR_LOG"
86+
87+
# Quick health check.
88+
sleep 2
89+
if kill -0 "$new_pid" 2>/dev/null; then
90+
echo " ✓ Bot is running"
91+
else
92+
echo " ✗ Bot died immediately — check log:"
93+
tail -20 "$STDERR_LOG"
94+
exit 1
95+
fi

0 commit comments

Comments
 (0)