collabrix/service-manager.sh
DGSoft 9daab3cdf4 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
2025-12-09 22:31:57 +01:00

321 lines
8.0 KiB
Bash
Executable File

#!/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