-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.sh
executable file
·169 lines (140 loc) · 4.67 KB
/
run.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
# Function to check if the server is running
function is_server_running() {
ps -p $SERVER_PID > /dev/null
}
# Function to start the server
function start_server() {
echo "Starting the server..."
python3 server.py &
SERVER_PID=$!
}
# Function to stop the server gracefully
function stop_server() {
kill $SERVER_PID
wait $SERVER_PID 2>/dev/null
}
# Check if the port is in use by another process
function is_port_in_use() {
netstat -tuln | grep ":$port_number\b" > /dev/null
}
# Check if config.txt exists, if not prompt the user to create one
if [[ ! -f config.txt ]]; then
read -p "Configuration file not found. Do you want to create one now? (y/n) " create_file
if [[ $create_file == "y" ]]; then
read -p "Enter user name: " user_name
read -p "Enter user password: " user_password
read -p "Enter patient ID (patient name): " patient_id
read -p "Enter user country (eg. US, UK): " user_country
read -p "Enter port number (default is 8000): " port_number
# Write configuration data to config.txt
echo "# Patient ID" > config.txt
echo "patient_id=$patient_id" >> config.txt
echo "" >> config.txt
echo "# User name" >> config.txt
echo "user_name=$user_name" >> config.txt
echo "" >> config.txt
echo "# User password" >> config.txt
echo "user_password=$user_password" >> config.txt
echo "" >> config.txt
echo "# Country" >> config.txt
echo "user_country=$user_country" >> config.txt
echo "" >> config.txt
echo "# Port number" >> config.txt
# Check if port_number is empty, set to default if so
if [[ -z $port_number ]]; then
echo "port_number=8000" >> config.txt
else
echo "port_number=$port_number" >> config.txt
fi
else
echo "Cannot start server without a configuration file. Exiting."
exit 1
fi
fi
# Read configuration data from config.txt
source config.txt
# Check if dependencies are already installed
echo "Checking for required dependencies..."
echo ""
if [[ ! -x "$(command -v pip)" ]]; then
echo "PIP is not installed, installing..."
sudo apt-get install python3-pip -y
fi
if [[ ! -x "$(command -v json)" ]]; then
echo "The json package is not installed, installing..."
pip install json
fi
if [[ ! -x "$(command -v http.server)" ]]; then
echo "The http.server package is not installed, installing..."
pip install http.server
fi
if [[ ! -x "$(command -v socketserver)" ]]; then
echo "The socketserver package is not installed, installing..."
pip install socketserver
fi
if [[ ! -x "$(command -v requests)" ]]; then
echo "The requests package is not installed, installing..."
pip install requests
fi
if [[ ! -x "$(command -v datetime)" ]]; then
echo "The datetime package is not installed, installing..."
pip install datetime
fi
# Check if the port is already in use
if is_port_in_use; then
echo "Port $port_number is already in use. Cannot start the server. Exiting."
exit 1
fi
# Kill processes running on port $port_number if needed
if is_server_running; then
echo "Killing existing processes running on port $port_number..."
stop_server
sleep 2
fi
# Start the server
start_server
# Start the data management file
echo "Starting the data management file..."
python3 data.py &
DATA_PID=$!
# Check if GUI is available
if [[ ! -z "$DISPLAY" ]]; then
echo "Opening index.html in a browser..."
xdg-open "http://localhost:$port_number/index.html"
else
echo "GUI is not available, open index.html in a browser to view the web interface."
fi
# Check the status of the servers and their availability
stty -echo
while true
do
SERVER_STATUS=$(ps -p $SERVER_PID -o comm=)
DATA_STATUS=$(ps -p $DATA_PID -o comm=)
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:$port_number/index.html)
clear
echo -e "\033[0m\033[1mServer status:"
echo -e "\033[0mServer.py: \033[32m$SERVER_STATUS"
echo -e "\033[0mData.py: \033[32m$DATA_STATUS"
echo -e "\033[0m"
echo "Availability on port $port_number:"
if [ $HTTP_STATUS -eq 200 ]
then
echo -e "index.html: \033[32mWorking\033[0m\n"
else
echo -e "index.html: \033[31mNot working\033[0m\n"
fi
echo "Press 'q' to exit."
# Check if the 'q' key has been pressed to exit the server
read -t 1 -n 1 input
if [[ $input = "q" ]]
then
stty echo
break
fi
sleep 1
done
# Terminate the server and data management file processes
echo -e "\nTerminating the server and data management file processes..."
stop_server
echo "Exiting the server."