-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.sh
38 lines (33 loc) · 995 Bytes
/
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
#!/bin/bash
# Check if python3 is installed
if ! command -v python3 &> /dev/null
then
echo "python3 could not be found"
exit
fi
# Start the Python server in the background
for port in {8000..8100}; do
# Check if the port is in use
if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null ; then
# Get the name of the command using the port
command_name=$(ps -p $(lsof -t -i:$port) -o comm=)
echo "Port $port is in use by $command_name"
continue
fi
# Start the server
echo "Starting server on port $port..."
python3 -m http.server $port > /tmp/config.log 2>&1 &
server_pid=$!
# Wait for the server to start
sleep 1
# Check if the server is running
if kill -0 $server_pid 2>/dev/null; then
echo -e "\nServer started. Press enter to stop the server."
read -p ""
kill $server_pid
echo "Server stopped."
break
else
echo "Failed to start server on port $port"
fi
done