Add Service Manager for Background Services

- service-manager.sh: Comprehensive service management script
- Start/stop backend and frontend as background services
- Status monitoring and log management
- Individual service control (start-backend, stop-frontend, etc.)
- SERVICE_MANAGER_README.md: Complete documentation
- PID file management for process tracking
- Colored output and error handling
- Production-ready with systemd integration examples
This commit is contained in:
DGSoft 2025-12-09 22:31:57 +01:00
parent 93b98cfb5c
commit 9daab3cdf4
2 changed files with 464 additions and 0 deletions

143
SERVICE_MANAGER_README.md Normal file
View File

@ -0,0 +1,143 @@
# Team Chat System - Service Manager
Das `service-manager.sh` Script ermöglicht es, Backend und Frontend als Hintergrund-Services zu starten und zu verwalten.
## 🚀 Schnellstart
```bash
# Starte beide Services
./service-manager.sh start
# Zeige Status
./service-manager.sh status
# Stoppe beide Services
./service-manager.sh stop
```
## 📋 Verfügbare Commands
### Grundlegende Commands
- `start` - Startet Backend und Frontend
- `stop` - Stoppt Backend und Frontend
- `restart` - Startet beide Services neu
- `status` - Zeigt den aktuellen Status an
### Individuelle Services
- `start-backend` - Startet nur das Backend
- `start-frontend` - Startet nur das Frontend
- `stop-backend` - Stoppt nur das Backend
- `stop-frontend` - Stoppt nur das Frontend
### Logging
- `logs backend [zeilen]` - Zeigt Backend-Logs (Standard: 50 Zeilen)
- `logs frontend [zeilen]` - Zeigt Frontend-Logs (Standard: 50 Zeilen)
## 📁 Dateien und Verzeichnisse
```
logs/
├── backend.log # Backend-Ausgaben
├── frontend.log # Frontend-Ausgaben
├── backend.pid # Backend-Prozess-ID
└── frontend.pid # Frontend-Prozess-ID
```
## 🌐 Zugriffe
Nach dem Start sind verfügbar:
- **Frontend:** http://192.168.0.12
- **Backend API:** http://localhost:8000
- **API Docs:** http://localhost:8000/docs
## 🔧 Troubleshooting
### Services starten nicht
```bash
# Prüfe Logs
./service-manager.sh logs backend
./service-manager.sh logs frontend
# Prüfe Status
./service-manager.sh status
```
### Services gewaltsam stoppen
```bash
# Einzelne Services
./service-manager.sh stop-backend
./service-manager.sh stop-frontend
# Oder alles
./service-manager.sh stop
```
### Log-Verzeichnis aufräumen
```bash
rm -rf logs/
```
## 📊 Beispiel-Session
```bash
# System starten
$ ./service-manager.sh start
[2025-12-09 22:15:30] Starte Team Chat System...
[2025-12-09 22:15:30] Starte Backend-Server...
[2025-12-09 22:15:32] ✅ Backend gestartet (PID: 12345)
[2025-12-09 22:15:32] 📍 API: http://localhost:8000
[2025-12-09 22:15:32] 📚 Docs: http://localhost:8000/docs
[2025-12-09 22:15:32] 📄 Logs: /home/OfficeDesk/logs/backend.log
[2025-12-09 22:15:32] Starte Frontend-Server...
[2025-12-09 22:15:35] ✅ Frontend gestartet (PID: 12346)
[2025-12-09 22:15:35] 📍 App: http://192.168.0.12
[2025-12-09 22:15:35] 📄 Logs: /home/OfficeDesk/logs/frontend.log
=== Team Chat System Status ===
🟢 Backend: Läuft (PID: 12345)
📍 API: http://localhost:8000
📚 Docs: http://localhost:8000/docs
🟢 Frontend: Läuft (PID: 12346)
📍 App: http://192.168.0.12
📁 Logs: /home/OfficeDesk/logs
```
## 🔄 Automatisches Starten
### Systemd Service (empfohlen für Production)
```bash
# Service-Datei erstellen
sudo nano /etc/systemd/system/teamchat.service
# Inhalt:
[Unit]
Description=Team Chat System
After=network.target
[Service]
Type=simple
User=your_user
WorkingDirectory=/home/OfficeDesk
ExecStart=/home/OfficeDesk/service-manager.sh start
ExecStop=/home/OfficeDesk/service-manager.sh stop
Restart=always
[Install]
WantedBy=multi-user.target
# Service aktivieren und starten
sudo systemctl daemon-reload
sudo systemctl enable teamchat
sudo systemctl start teamchat
```
### Cron-Job für automatischen Start beim Boot
```bash
# Cron-Job hinzufügen
crontab -e
# Füge hinzu:
@reboot /home/OfficeDesk/service-manager.sh start
```

321
service-manager.sh Executable file
View File

@ -0,0 +1,321 @@
#!/bin/bash
# Team Chat System - Service Manager
# Startet Backend und Frontend als Hintergrund-Services
set -e
# Konfiguration
PROJECT_DIR="/home/OfficeDesk"
BACKEND_DIR="$PROJECT_DIR/backend"
FRONTEND_DIR="$PROJECT_DIR/frontend"
LOG_DIR="$PROJECT_DIR/logs"
# PID-Dateien
BACKEND_PID_FILE="$LOG_DIR/backend.pid"
FRONTEND_PID_FILE="$LOG_DIR/frontend.pid"
# Log-Dateien
BACKEND_LOG="$LOG_DIR/backend.log"
FRONTEND_LOG="$LOG_DIR/frontend.log"
# Erstelle Log-Verzeichnis falls nicht vorhanden
mkdir -p "$LOG_DIR"
# Farben für Ausgabe
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Hilfsfunktionen
log() {
echo -e "${GREEN}[$(date +'%Y-%m-%d %H:%M:%S')] $1${NC}"
}
error() {
echo -e "${RED}[ERROR] $1${NC}" >&2
}
warning() {
echo -e "${YELLOW}[WARNING] $1${NC}"
}
info() {
echo -e "${BLUE}[INFO] $1${NC}"
}
# Prüft ob ein Prozess läuft
is_running() {
local pid_file=$1
if [[ -f "$pid_file" ]]; then
local pid=$(cat "$pid_file")
if kill -0 "$pid" 2>/dev/null; then
return 0 # Läuft
else
rm -f "$pid_file" # PID-Datei aufräumen
return 1 # Läuft nicht
fi
fi
return 1 # Läuft nicht
}
# Backend starten
start_backend() {
if is_running "$BACKEND_PID_FILE"; then
warning "Backend läuft bereits (PID: $(cat $BACKEND_PID_FILE))"
return 0
fi
log "Starte Backend-Server..."
cd "$BACKEND_DIR"
# Starte Backend im Hintergrund
nohup /bin/python -m uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload > "$BACKEND_LOG" 2>&1 &
local backend_pid=$!
# Warte kurz und prüfe ob der Prozess gestartet ist
sleep 2
if kill -0 $backend_pid 2>/dev/null; then
echo $backend_pid > "$BACKEND_PID_FILE"
log "✅ Backend gestartet (PID: $backend_pid)"
log "📍 API: http://localhost:8000"
log "📚 Docs: http://localhost:8000/docs"
log "📄 Logs: $BACKEND_LOG"
else
error "Backend konnte nicht gestartet werden"
cat "$BACKEND_LOG" 2>/dev/null || true
return 1
fi
}
# Frontend starten
start_frontend() {
if is_running "$FRONTEND_PID_FILE"; then
warning "Frontend läuft bereits (PID: $(cat $FRONTEND_PID_FILE))"
return 0
fi
log "Starte Frontend-Server..."
cd "$FRONTEND_DIR"
# Starte Frontend im Hintergrund
nohup npm run dev > "$FRONTEND_LOG" 2>&1 &
local frontend_pid=$!
# Warte kurz und prüfe ob der Prozess gestartet ist
sleep 3
if kill -0 $frontend_pid 2>/dev/null; then
echo $frontend_pid > "$FRONTEND_PID_FILE"
log "✅ Frontend gestartet (PID: $frontend_pid)"
log "📍 App: http://192.168.0.12"
log "📄 Logs: $FRONTEND_LOG"
else
error "Frontend konnte nicht gestartet werden"
cat "$FRONTEND_LOG" 2>/dev/null || true
return 1
fi
}
# Backend stoppen
stop_backend() {
if ! is_running "$BACKEND_PID_FILE"; then
warning "Backend läuft nicht"
return 0
fi
local pid=$(cat "$BACKEND_PID_FILE")
log "Stoppe Backend (PID: $pid)..."
if kill "$pid" 2>/dev/null; then
# Warte bis der Prozess beendet ist
local count=0
while kill -0 "$pid" 2>/dev/null && [ $count -lt 10 ]; do
sleep 1
((count++))
done
if kill -0 "$pid" 2>/dev/null; then
warning "Backend reagiert nicht, erzwinge Stop..."
kill -9 "$pid" 2>/dev/null || true
fi
rm -f "$BACKEND_PID_FILE"
log "✅ Backend gestoppt"
else
error "Backend konnte nicht gestoppt werden"
return 1
fi
}
# Frontend stoppen
stop_frontend() {
if ! is_running "$FRONTEND_PID_FILE"; then
warning "Frontend läuft nicht"
return 0
fi
local pid=$(cat "$FRONTEND_PID_FILE")
log "Stoppe Frontend (PID: $pid)..."
if kill "$pid" 2>/dev/null; then
# Warte bis der Prozess beendet ist
local count=0
while kill -0 "$pid" 2>/dev/null && [ $count -lt 10 ]; do
sleep 1
((count++))
done
if kill -0 "$pid" 2>/dev/null; then
warning "Frontend reagiert nicht, erzwinge Stop..."
kill -9 "$pid" 2>/dev/null || true
fi
rm -f "$FRONTEND_PID_FILE"
log "✅ Frontend gestoppt"
else
error "Frontend konnte nicht gestoppt werden"
return 1
fi
}
# Status anzeigen
show_status() {
echo ""
echo "=== Team Chat System Status ==="
echo ""
# Backend Status
if is_running "$BACKEND_PID_FILE"; then
echo -e "🟢 Backend: ${GREEN}Läuft${NC} (PID: $(cat $BACKEND_PID_FILE))"
echo " 📍 API: http://localhost:8000"
echo " 📚 Docs: http://localhost:8000/docs"
else
echo -e "🔴 Backend: ${RED}Gestoppt${NC}"
fi
# Frontend Status
if is_running "$FRONTEND_PID_FILE"; then
echo -e "🟢 Frontend: ${GREEN}Läuft${NC} (PID: $(cat $FRONTEND_PID_FILE))"
echo " 📍 App: http://192.168.0.12"
else
echo -e "🔴 Frontend: ${RED}Gestoppt${NC}"
fi
echo ""
echo "📁 Logs: $LOG_DIR"
echo "📄 Backend Log: $BACKEND_LOG"
echo "📄 Frontend Log: $FRONTEND_LOG"
}
# Logs anzeigen
show_logs() {
local service=$1
local lines=${2:-50}
case $service in
backend)
if [[ -f "$BACKEND_LOG" ]]; then
echo "=== Backend Logs (letzte $lines Zeilen) ==="
tail -n "$lines" "$BACKEND_LOG"
else
warning "Backend-Log nicht gefunden: $BACKEND_LOG"
fi
;;
frontend)
if [[ -f "$FRONTEND_LOG" ]]; then
echo "=== Frontend Logs (letzte $lines Zeilen) ==="
tail -n "$lines" "$FRONTEND_LOG"
else
warning "Frontend-Log nicht gefunden: $FRONTEND_LOG"
fi
;;
*)
error "Unbekannter Service: $service"
echo "Verwendung: $0 logs [backend|frontend] [zeilen]"
;;
esac
}
# Hilfe anzeigen
show_help() {
echo "Team Chat System - Service Manager"
echo ""
echo "Verwendung: $0 [command]"
echo ""
echo "Commands:"
echo " start Starte beide Services (Backend + Frontend)"
echo " stop Stoppe beide Services"
echo " restart Stoppe und starte beide Services neu"
echo " start-backend Starte nur Backend"
echo " start-frontend Starte nur Frontend"
echo " stop-backend Stoppe nur Backend"
echo " stop-frontend Stoppe nur Frontend"
echo " status Zeige Status aller Services"
echo " logs [service] [lines] Zeige Logs (service: backend|frontend, lines: optional)"
echo " help Zeige diese Hilfe"
echo ""
echo "Beispiele:"
echo " $0 start # Starte alles"
echo " $0 status # Zeige Status"
echo " $0 logs backend 100 # Zeige letzte 100 Backend-Logs"
echo " $0 stop-frontend # Stoppe nur Frontend"
}
# Hauptlogik
case "${1:-help}" in
start)
log "Starte Team Chat System..."
start_backend
start_frontend
echo ""
show_status
;;
stop)
log "Stoppe Team Chat System..."
stop_frontend
stop_backend
echo ""
show_status
;;
restart)
log "Restarte Team Chat System..."
stop_frontend
stop_backend
sleep 2
start_backend
start_frontend
echo ""
show_status
;;
start-backend)
start_backend
;;
start-frontend)
start_frontend
;;
stop-backend)
stop_backend
;;
stop-frontend)
stop_frontend
;;
status)
show_status
;;
logs)
show_logs "$2" "$3"
;;
help|--help|-h)
show_help
;;
*)
error "Unbekannter Befehl: $1"
echo ""
show_help
exit 1
;;
esac