brendon-ai commited on
Commit
4974b02
Β·
verified Β·
1 Parent(s): 77b4dba

Update startup.sh

Browse files
Files changed (1) hide show
  1. startup.sh +44 -75
startup.sh CHANGED
@@ -1,89 +1,58 @@
1
  #!/bin/bash
2
 
 
 
 
3
  echo "=== Starting Ollama API Server ==="
4
 
5
- # Function to check if Ollama is running
6
- check_ollama() {
7
- curl -s http://localhost:11434/api/version > /dev/null
8
- return $?
9
- }
10
 
11
- # Function to start Ollama server
12
- start_ollama() {
13
- echo "Starting Ollama server..."
14
- su - ollama -c "ollama serve" &
15
- OLLAMA_PID=$!
16
- echo "Ollama server started with PID: $OLLAMA_PID"
17
-
18
- # Wait for Ollama to be ready
19
- echo "Waiting for Ollama server to be ready..."
20
- for i in {1..30}; do
21
- if check_ollama; then
22
- echo "βœ… Ollama server is ready!"
23
- break
24
- fi
25
- echo "⏳ Waiting for Ollama server... ($i/30)"
26
- sleep 2
27
- done
28
-
29
- if ! check_ollama; then
30
- echo "❌ Ollama server failed to start properly"
31
- exit 1
32
- fi
33
- }
34
 
35
- # Function to start FastAPI
36
- start_fastapi() {
37
- echo "Starting FastAPI server..."
38
- python app.py &
39
- FASTAPI_PID=$!
40
- echo "FastAPI server started with PID: $FASTAPI_PID"
41
- }
42
 
43
- # Function to handle shutdown
44
- cleanup() {
45
- echo "Shutting down services..."
46
- if [ ! -z "$FASTAPI_PID" ]; then
47
- kill $FASTAPI_PID 2>/dev/null
48
- fi
49
- if [ ! -z "$OLLAMA_PID" ]; then
50
- kill $OLLAMA_PID 2>/dev/null
51
- fi
52
- exit 0
53
  }
54
 
55
- # Set up signal handlers
56
- trap cleanup SIGTERM SIGINT
 
 
57
 
58
- # Create necessary directories
59
- mkdir -p /home/ollama/.ollama/models
60
- chown -R ollama:ollama /home/ollama/.ollama
 
 
 
 
 
 
 
 
61
 
62
- # Start Ollama server
63
- start_ollama
64
 
65
- # Start FastAPI server
66
- start_fastapi
 
67
 
68
- # Keep the script running and monitor processes
69
- echo "πŸš€ Both services are running!"
70
- echo "- Ollama server: http://localhost:11434"
71
- echo "- FastAPI server: http://localhost:7860"
72
- echo "- API Documentation: http://localhost:7860/docs"
73
 
74
- # Monitor both processes
75
- while true; do
76
- # Check if Ollama is still running
77
- if ! kill -0 $OLLAMA_PID 2>/dev/null; then
78
- echo "❌ Ollama server stopped unexpectedly"
79
- exit 1
80
- fi
81
-
82
- # Check if FastAPI is still running
83
- if ! kill -0 $FASTAPI_PID 2>/dev/null; then
84
- echo "❌ FastAPI server stopped unexpectedly"
85
- exit 1
86
- fi
87
-
88
- sleep 10
89
- done
 
1
  #!/bin/bash
2
 
3
+ # Exit on any error
4
+ set -e
5
+
6
  echo "=== Starting Ollama API Server ==="
7
 
8
+ # Set Ollama environment variables
9
+ export OLLAMA_HOST=0.0.0.0:11434
10
+ export OLLAMA_MODELS=/app/.ollama/models
11
+ export OLLAMA_HOME=/app/.ollama
12
+ export OLLAMA_ORIGINS="*"
13
 
14
+ # Create models directory if it doesn't exist
15
+ mkdir -p /app/.ollama/models
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
+ # Start Ollama server in the background
18
+ echo "Starting Ollama server..."
19
+ ollama serve &
20
+ OLLAMA_PID=$!
21
+ echo "Ollama server started with PID: $OLLAMA_PID"
 
 
22
 
23
+ # Function to check if Ollama is ready
24
+ check_ollama() {
25
+ curl -s http://localhost:11434/api/tags > /dev/null 2>&1
26
+ return $?
 
 
 
 
 
 
27
  }
28
 
29
+ # Wait for Ollama server to be ready
30
+ echo "Waiting for Ollama server to be ready..."
31
+ MAX_WAIT=60
32
+ WAIT_COUNT=0
33
 
34
+ while ! check_ollama; do
35
+ if [ $WAIT_COUNT -ge $MAX_WAIT ]; then
36
+ echo "❌ Ollama server failed to start within $MAX_WAIT seconds"
37
+ kill $OLLAMA_PID 2>/dev/null || true
38
+ exit 1
39
+ fi
40
+
41
+ echo "⏳ Waiting for Ollama server... ($((WAIT_COUNT + 1))/$MAX_WAIT)"
42
+ sleep 1
43
+ WAIT_COUNT=$((WAIT_COUNT + 1))
44
+ done
45
 
46
+ echo "βœ… Ollama server is ready!"
 
47
 
48
+ # Optional: Pull a small model for testing
49
+ echo "Pulling a lightweight model (this may take a few minutes)..."
50
+ ollama pull tinyllama &
51
 
52
+ # Start the main application
53
+ echo "Starting main application..."
54
+ python3 app.py
 
 
55
 
56
+ # If we reach here, something went wrong with the main app
57
+ echo "❌ Main application exited unexpectedly"
58
+ kill $OLLAMA_PID 2>/dev/null || true