-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathserver.sh
63 lines (57 loc) · 1.3 KB
/
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
#!/bin/bash
start() {
nohup ./venv/bin/uvicorn main:app --port 8002 > server.log 2>&1 &
echo $! > server.pid
echo "Server started"
if ! ps -p $! > /dev/null; then
echo "Failed to start server."
return 1
fi
}
stop() {
if [ ! -f server.pid ]; then
echo "Server is not running."
return
fi
local pid=$(cat server.pid)
kill -15 "$pid"
local count=0
while ps -p "$pid" > /dev/null; do
sleep 1
count=$((count + 1))
if [ "$count" -eq 30 ]; then
echo "Server did not shut down gracefully; force-stopping."
kill -9 "$pid"
break
fi
done
rm server.pid
echo "Server stopped"
}
restart() {
echo "Restarting server..."
stop
start
}
update() {
echo "Updating server..."
stop
if [ -f "./venv/bin/activate" ]; then
source ./venv/bin/activate
else
echo "Virtual environment not found. Please ensure the virtual environment is correctly set up."
return 1
fi
if ! pip install -r requirements.txt; then
echo "Failed to install requirements."
return 1
fi
start
}
case "$1" in
start) start ;;
stop) stop ;;
restart) restart ;;
update) update ;;
*) echo "Usage: $0 {start|stop|restart|update}" ;;
esac