Skip to content

Commit 4063612

Browse files
committed
Enhance start-app.sh for cross-platform compatibility with Python commands and process management
1 parent 619a35b commit 4063612

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

scripts/start-app.sh

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ fi
1414

1515
echo "Starting API (Flask) server..."
1616

17-
python3 -m venv venv
18-
source venv/bin/activate
17+
# Check OS and use appropriate Python command
18+
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then
19+
# Windows
20+
py -m venv venv
21+
source venv/Scripts/activate || . venv/Scripts/activate
22+
else
23+
# macOS/Linux
24+
python3 -m venv venv
25+
source venv/bin/activate || . venv/bin/activate
26+
fi
27+
1928
pip install -r server/requirements.txt
2029
cd server || {
2130
echo "Error: server directory not found"
@@ -24,7 +33,13 @@ cd server || {
2433
}
2534
export FLASK_DEBUG=1
2635
export FLASK_PORT=5100
27-
python app.py &
36+
37+
# Use appropriate Python command based on OS
38+
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then
39+
py app.py &
40+
else
41+
python3 app.py &
42+
fi
2843

2944
# Store the Python server process ID
3045
SERVER_PID=$!
@@ -53,13 +68,43 @@ echo "Ctl-C to stop the servers"
5368
# Function to handle script termination
5469
cleanup() {
5570
echo "Shutting down servers..."
56-
kill $SERVER_PID
57-
kill $CLIENT_PID
71+
72+
# Kill processes and their child processes
73+
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "win32" ]]; then
74+
taskkill //F //T //PID $SERVER_PID 2>/dev/null
75+
taskkill //F //T //PID $CLIENT_PID 2>/dev/null
76+
else
77+
# Send SIGTERM first to allow graceful shutdown
78+
kill -TERM $SERVER_PID 2>/dev/null
79+
kill -TERM $CLIENT_PID 2>/dev/null
80+
81+
# Wait briefly for graceful shutdown
82+
sleep 2
83+
84+
# Then force kill if still running
85+
if ps -p $SERVER_PID > /dev/null 2>&1; then
86+
pkill -P $SERVER_PID 2>/dev/null
87+
kill -9 $SERVER_PID 2>/dev/null
88+
fi
89+
90+
if ps -p $CLIENT_PID > /dev/null 2>&1; then
91+
pkill -P $CLIENT_PID 2>/dev/null
92+
kill -9 $CLIENT_PID 2>/dev/null
93+
fi
94+
fi
95+
96+
# Deactivate virtual environment if active
97+
if [[ -n "${VIRTUAL_ENV}" ]]; then
98+
deactivate
99+
fi
100+
101+
# Return to initial directory
102+
cd "$INITIAL_DIR"
58103
exit 0
59104
}
60105

61-
# Trap SIGINT (Ctrl+C) and SIGTERM
62-
trap cleanup SIGINT SIGTERM
106+
# Trap multiple signals
107+
trap cleanup SIGINT SIGTERM SIGQUIT EXIT
63108

64109
# Keep the script running
65110
wait

0 commit comments

Comments
 (0)