-
Notifications
You must be signed in to change notification settings - Fork 0
/
alex-server.sh
executable file
·128 lines (115 loc) · 2.76 KB
/
alex-server.sh
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
#!/bin/bash
# Define constants
PID_FILE="/home/pegasus/.alex_server"
ALEX_PID_FILE="/home/pegasus/.alex"
SCRIPT_DIR="/home/pegasus/development/AlexBaseAPI/"
LOG_FILE="output.log"
# Function to display usage information
usage() {
echo "Usage: alex-server [command] [options]"
echo
echo "Commands:"
echo " start Start the Alex server if it's not running."
echo " stop Stop the Alex server if it's running."
echo " reload Reload the Alex server (stop and then start)."
echo " show Will show the Alex Server if Its Active."
echo " train Will train all the ai related features of The Alex Base Server."
echo " --help, -h Display this help message."
echo
echo "Options for start and reload:"
echo " [args] Arguments to pass to the server's main.py script."
echo
echo "The Alex server listens on port 1178 of 0.0.0.0."
echo "Logs are saved to $LOG_FILE."
exit 0
}
use_env() {
cd "$SCRIPT_DIR"
source .venv/bin/activate
}
# Function to kill the server
killit() {
if [ ! -f "$PID_FILE" ]; then
echo "The server is not running."
exit 1
fi
PID=$(cat "$PID_FILE")
if ! [[ "$PID" =~ ^[0-9]+$ ]]; then
echo "PID is not valid: $PID"
exit 1
fi
kill "$PID"
if [ $? -eq 0 ]; then
echo "Alex Server ($PID) terminated successfully."
rm "$PID_FILE"
else
echo "Failed to terminate Alex Server ($PID)."
fi
}
# Function to start the server
startit() {
if [ -f "$PID_FILE" ]; then
echo "The server is already running"
else
shift
echo "Starting server"
use_env
nohup python main.py "$@" > "$LOG_FILE" 2>&1 &
sleep 4
echo "Alex Server started with PID $(cat $PID_FILE)."
fi
}
show() {
if [ ! -f "$PID_FILE" ]; then
echo "The server is not running."
exit 1
fi
if [ -f "$ALEX_PID_FILE" ]; then
echo "Alex"
top -p $(cat "$PID_FILE"),$(cat "$ALEX_PID_FILE") -d 0.2 -E g -e m -H
else
top -p $(cat "$PID_FILE") -d 0.1 -E g -e m -H
fi
}
clear() {
rm -f "$PID_FILE"
rm -f "$ALEX_PID_FILE"
}
train() {
use_env
if [ -f "$PID_FILE" ]; then
killit
fi
python train.py $1
startit
}
# Main script logic
case "$1" in
reload)
echo "Reloading the server."
killit
startit
;;
stop)
killit
clear
;;
show)
show
;;
clear)
clear
;;
--help|-h)
usage
;;
start)
startit
;;
train)
train $2
;;
*)
echo "$1" is not a valid command. See -h/--help for help
;;
esac