brendon-ai commited on
Commit
1f9f492
Β·
verified Β·
1 Parent(s): 6558700

Create startup.sh

Browse files
Files changed (1) hide show
  1. startup.sh +89 -0
startup.sh ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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