#!/bin/bash # IFM Telegram Bot - Process Manager (for testing/analysis) # Usage: ./bot.sh {start|stop|restart|status|logs|tail} BOT_DIR="$(cd "$(dirname "$0")" && pwd)" JAR_FILE="$BOT_DIR/target/ifm-telegram-bot-1.0.0-jar-with-dependencies.jar" PID_FILE="$BOT_DIR/bot.pid" LOG_DIR="$BOT_DIR/logs" TMUX_SESSION="ifm-bot" STOP_TIMEOUT=10 GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' NC='\033[0m' get_pid() { if [ -f "$PID_FILE" ]; then local pid pid=$(cat "$PID_FILE") if kill -0 "$pid" 2>/dev/null; then echo "$pid" return 0 fi rm -f "$PID_FILE" fi return 1 } do_start() { local pid if pid=$(get_pid); then echo -e "${YELLOW}Bot is already running (PID: $pid)${NC}" return 1 fi if [ ! -f "$JAR_FILE" ]; then echo -e "${RED}JAR not found: $JAR_FILE${NC}" echo -e "${YELLOW}Run: mvn clean package -DskipTests${NC}" return 1 fi mkdir -p "$LOG_DIR" # Kill any stale tmux session tmux kill-session -t "$TMUX_SESSION" 2>/dev/null # Start bot inside a detached tmux session (UTF-8 for Thai text) tmux new-session -d -s "$TMUX_SESSION" \ "cd '$BOT_DIR' && LANG=C.UTF-8 LC_ALL=C.UTF-8 exec java -Dfile.encoding=UTF-8 -jar '$JAR_FILE' >> '$LOG_DIR/console.log' 2>&1" sleep 3 # Find the actual java PID from the tmux session local bot_pid bot_pid=$(tmux list-panes -t "$TMUX_SESSION" -F '#{pane_pid}' 2>/dev/null) # The pane_pid is the shell; find the java child local java_pid java_pid=$(pgrep -P "$bot_pid" -f java 2>/dev/null | head -1) if [ -n "$java_pid" ] && kill -0 "$java_pid" 2>/dev/null; then echo "$java_pid" > "$PID_FILE" echo -e "${GREEN}Bot started (PID: $java_pid)${NC}" elif [ -n "$bot_pid" ] && kill -0 "$bot_pid" 2>/dev/null; then # Fallback: use the tmux pane pid echo "$bot_pid" > "$PID_FILE" echo -e "${GREEN}Bot started (PID: $bot_pid)${NC}" else echo -e "${RED}Bot failed to start. Check logs:${NC}" tail -5 "$LOG_DIR/console.log" 2>/dev/null return 1 fi } do_stop() { local pid if ! pid=$(get_pid); then echo -e "${YELLOW}Bot is not running${NC}" # Cleanup stale tmux just in case tmux kill-session -t "$TMUX_SESSION" 2>/dev/null return 0 fi echo -e "${YELLOW}Stopping bot (PID: $pid)...${NC}" kill "$pid" for i in $(seq 1 "$STOP_TIMEOUT"); do if ! kill -0 "$pid" 2>/dev/null; then rm -f "$PID_FILE" tmux kill-session -t "$TMUX_SESSION" 2>/dev/null echo -e "${GREEN}Bot stopped${NC}" return 0 fi sleep 1 done echo -e "${RED}Force killing...${NC}" kill -9 "$pid" 2>/dev/null rm -f "$PID_FILE" tmux kill-session -t "$TMUX_SESSION" 2>/dev/null echo -e "${GREEN}Bot stopped (forced)${NC}" } do_status() { local pid if pid=$(get_pid); then local uptime uptime=$(ps -o etime= -p "$pid" 2>/dev/null | xargs) local mem mem=$(ps -o rss= -p "$pid" 2>/dev/null | awk '{printf "%.1f MB", $1/1024}') echo -e "${GREEN}Bot is running${NC}" echo " PID: $pid" echo " Uptime: $uptime" echo " Memory: $mem" else echo -e "${RED}Bot is not running${NC}" return 1 fi } do_logs() { if [ -f "$LOG_DIR/console.log" ]; then tail -50 "$LOG_DIR/console.log" else echo -e "${YELLOW}No log file found${NC}" fi } do_tail() { if [ -f "$LOG_DIR/console.log" ]; then tail -f "$LOG_DIR/console.log" else echo -e "${YELLOW}No log file found. Start the bot first.${NC}" fi } case "$1" in start) do_start ;; stop) do_stop ;; restart) do_stop && sleep 1 && do_start ;; status) do_status ;; logs) do_logs ;; tail) do_tail ;; *) echo "Usage: $0 {start|stop|restart|status|logs|tail}" echo "" echo " start - Start bot in background (tmux)" echo " stop - Stop bot" echo " restart - Restart bot" echo " status - Show PID, uptime, memory" echo " logs - Show last 50 lines of log" echo " tail - Follow log in real-time (Ctrl+C to exit)" exit 1 ;; esac