-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstart_services.sh
51 lines (45 loc) · 1.38 KB
/
start_services.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
#!/bin/bash
# Print the environment variable for debugging
echo "OLLAMA_API_KEY: '$OLLAMA_API_KEY'"
# Ensure required environment variables are set
if [ -z "$OLLAMA_API_KEY" ]; then
echo "OLLAMA_API_KEY is not set. Exiting."
exit 1
fi
# Start ollama in the background
ollama serve &
OLLAMA_PID=$!
# Start caddy in the background
caddy run --config /etc/caddy/Caddyfile &
CADDY_PID=$!
# Function to check process status
check_process() {
wait $1
STATUS=$?
if [ $STATUS -ne 0 ]; then
echo "Process $2 ($1) has exited with status $STATUS"
exit $STATUS
fi
}
# Handle shutdown signals
trap "kill $OLLAMA_PID $CADDY_PID; exit 0" SIGTERM SIGINT
# Wait for both services to start and monitor them
while true; do
if ! ps -p $OLLAMA_PID > /dev/null; then
echo "Ollama service is not running, checking for exit status"
check_process $OLLAMA_PID "Ollama"
# Only restart if check_process hasn't exited the script
echo "Starting Ollama now"
ollama serve &
OLLAMA_PID=$!
fi
if ! ps -p $CADDY_PID > /dev/null; then
echo "Caddy service is not running, checking for exit status"
check_process $CADDY_PID "Caddy"
# Only restart if check_process hasn't exited the script
echo "Starting Caddy now"
caddy run --config /etc/caddy/Caddyfile &
CADDY_PID=$!
fi
sleep 1
done