-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbotctl
More file actions
executable file
·148 lines (125 loc) · 4.29 KB
/
Copy pathbotctl
File metadata and controls
executable file
·148 lines (125 loc) · 4.29 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
# Bot management script for Iris - Executive Assistant
BOT_DIR="/home/executive-assistant"
PID_FILE="/tmp/iris-bot.pid"
LOG_FILE="/tmp/iris-bot.log"
VENV_DIR="$BOT_DIR/venv"
RUN_USER="iris"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
case "$1" in
start)
if [ -f "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") 2>/dev/null; then
echo -e "${YELLOW}Bot already running (PID: $(cat $PID_FILE))${NC}"
exit 1
fi
cd "$BOT_DIR"
# Create venv if it doesn't exist
if [ ! -d "$VENV_DIR" ]; then
echo -e "${YELLOW}Creating virtual environment...${NC}"
python3 -m venv "$VENV_DIR"
source "$VENV_DIR/bin/activate"
pip install -r requirements.txt
else
source "$VENV_DIR/bin/activate"
fi
# Load environment variables
if [ -f "$BOT_DIR/.env" ]; then
export $(grep -v '^#' "$BOT_DIR/.env" | xargs)
fi
# Build env string from .env file
ENV_EXPORTS=""
if [ -f "$BOT_DIR/.env" ]; then
ENV_EXPORTS=$(grep -v '^#' "$BOT_DIR/.env" | grep -v '^$' | sed 's/^/export /' | tr '\n' ';')
fi
# Start bot in background as iris user
sudo -u "$RUN_USER" bash -c "export PATH=\"/home/iris/.local/node_modules/.bin:\$PATH\"; $ENV_EXPORTS source \"$VENV_DIR/bin/activate\" && cd \"$BOT_DIR\" && nohup python bot.py >> \"$LOG_FILE\" 2>&1 & echo \$!" > "$PID_FILE"
# Wait a moment and get actual python PID
sleep 1
PYTHON_PID=$(pgrep -u "$RUN_USER" -f "python bot.py" | tail -1)
if [ -n "$PYTHON_PID" ]; then
echo "$PYTHON_PID" > "$PID_FILE"
fi
sleep 2
if kill -0 $(cat "$PID_FILE") 2>/dev/null; then
echo -e "${GREEN}Bot started (PID: $(cat $PID_FILE))${NC}"
else
echo -e "${RED}Bot failed to start. Check logs: $LOG_FILE${NC}"
rm -f "$PID_FILE"
exit 1
fi
;;
stop)
# Kill by PID file
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if kill -0 "$PID" 2>/dev/null; then
kill "$PID" 2>/dev/null
sleep 1
fi
rm -f "$PID_FILE"
fi
# Also kill any orphan bot.py processes owned by RUN_USER
pkill -u "$RUN_USER" -f "python bot.py" 2>/dev/null
sleep 1
pkill -9 -u "$RUN_USER" -f "python bot.py" 2>/dev/null
echo -e "${GREEN}Bot stopped${NC}"
;;
restart)
$0 stop
sleep 2
$0 start
;;
status)
if [ -f "$PID_FILE" ] && kill -0 $(cat "$PID_FILE") 2>/dev/null; then
echo -e "${GREEN}Running (PID: $(cat $PID_FILE))${NC}"
# Show memory usage
PID=$(cat "$PID_FILE")
MEM=$(ps -o rss= -p $PID 2>/dev/null | awk '{print int($1/1024)"MB"}')
echo -e "Memory: $MEM"
# Show uptime
STARTED=$(ps -o lstart= -p $PID 2>/dev/null)
echo -e "Started: $STARTED"
else
echo -e "${RED}Not running${NC}"
exit 1
fi
;;
logs)
if [ -f "$LOG_FILE" ]; then
tail -f "$LOG_FILE"
else
echo -e "${YELLOW}No log file found${NC}"
fi
;;
setup)
echo -e "${YELLOW}Setting up Executive Assistant bot...${NC}"
cd "$BOT_DIR"
# Create venv
python3 -m venv "$VENV_DIR"
source "$VENV_DIR/bin/activate"
# Install dependencies
pip install -r requirements.txt
echo -e "${GREEN}Setup complete!${NC}"
echo ""
echo "Next steps:"
echo "1. Edit .env with your Discord token and other credentials"
echo "2. Run: ./botctl start"
;;
*)
echo "Iris - Executive Assistant Bot"
echo ""
echo "Usage: botctl {start|stop|restart|status|logs|setup}"
echo ""
echo "Commands:"
echo " start - Start Iris"
echo " stop - Stop Iris"
echo " restart - Restart Iris"
echo " status - Check if Iris is running"
echo " logs - Tail the log file"
echo " setup - First-time setup (create venv, install deps)"
;;
esac