BotDeepSeek/restart_bot.sh

42 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
set -e
BOT_DIR="$(cd "$(dirname "$0")" && pwd)"
LOG_FILE="$BOT_DIR/trading_bot.log"
echo "=== IB Trading Bot Starter ==="
echo "BOT_DIR: $BOT_DIR"
# Step 1: Kill any existing bot process (TERM first, escalate to KILL if stuck)
PIDS=$(pgrep -f "main.py" || true)
if [ -n "$PIDS" ]; then
echo "Old bot PID(s): $PIDS, sending SIGTERM..."
pkill -f "main.py" 2>/dev/null || true
for _ in $(seq 1 10); do
if ! pgrep -f "main.py" >/dev/null 2>&1; then break; fi
sleep 1
done
if pgrep -f "main.py" >/dev/null 2>&1; then
echo "Still alive after 10s, sending SIGKILL..."
pkill -9 -f "main.py" 2>/dev/null || true
sleep 1
fi
fi
echo "[1/2] Stopped old bot process (if any)"
# Step 2: Start the trading bot (setsid so it survives this shell exiting)
echo "[2/2] Starting trading bot..."
setsid nohup "$BOT_DIR/.venv/bin/python" "$BOT_DIR/main.py" >> "$LOG_FILE" 2>&1 < /dev/null &
BOT_PID=$!
echo "Bot PID: $BOT_PID"
# Wait a few seconds and check
sleep 5
if kill -0 "$BOT_PID" 2>/dev/null; then
echo "=== Bot started successfully (PID: $BOT_PID) ==="
echo "Log: tail -f $LOG_FILE"
else
echo "=== Bot failed to start, check log ==="
tail -5 "$LOG_FILE"
fi