#!/bin/bash # Install Multi-Model Chatbot as systemd service # This script must be run with sudo privileges set -e # Exit on error # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Check if running as root if [ "$EUID" -ne 0 ]; then echo -e "${RED}Error: This script must be run as root (use sudo)${NC}" exit 1 fi # Get the actual user (not root) ACTUAL_USER=${SUDO_USER:-$USER} if [ "$ACTUAL_USER" = "root" ]; then echo -e "${RED}Error: Please run with sudo as a regular user, not as root${NC}" exit 1 fi # Get current directory CURRENT_DIR=$(pwd) SERVICE_FILE="chatbot.service" echo -e "${GREEN}=== Multi-Model Chatbot Service Installer ===${NC}" echo "" # Check if service file exists if [ ! -f "$SERVICE_FILE" ]; then echo -e "${RED}Error: $SERVICE_FILE not found in current directory${NC}" exit 1 fi # Check if venv exists if [ ! -d "venv" ]; then echo -e "${YELLOW}Warning: venv directory not found${NC}" echo -e "${YELLOW}Please create virtual environment first:${NC}" echo " python -m venv venv" echo " source venv/bin/activate" echo " pip install -r requirements-local.txt" exit 1 fi # Check if app.py exists if [ ! -f "app.py" ]; then echo -e "${RED}Error: app.py not found in current directory${NC}" exit 1 fi # Create customized service file TEMP_SERVICE="/tmp/chatbot-temp.service" cp "$SERVICE_FILE" "$TEMP_SERVICE" # Replace placeholders sed -i "s|YOUR_USERNAME|$ACTUAL_USER|g" "$TEMP_SERVICE" sed -i "s|/path/to/simple-chatbot-gradio|$CURRENT_DIR|g" "$TEMP_SERVICE" echo -e "${GREEN}✓ Service file configured:${NC}" echo " User: $ACTUAL_USER" echo " Directory: $CURRENT_DIR" echo "" # Create log directory if not exists LOG_DIR="/var/log" if [ ! -d "$LOG_DIR" ]; then mkdir -p "$LOG_DIR" fi # Set log file permissions touch /var/log/chatbot.log touch /var/log/chatbot-error.log chown $ACTUAL_USER:$ACTUAL_USER /var/log/chatbot.log chown $ACTUAL_USER:$ACTUAL_USER /var/log/chatbot-error.log chmod 644 /var/log/chatbot.log chmod 644 /var/log/chatbot-error.log echo -e "${GREEN}✓ Log files created${NC}" # Copy service file to systemd directory cp "$TEMP_SERVICE" /etc/systemd/system/chatbot.service rm "$TEMP_SERVICE" echo -e "${GREEN}✓ Service file installed to /etc/systemd/system/chatbot.service${NC}" # Reload systemd daemon systemctl daemon-reload echo -e "${GREEN}✓ Systemd daemon reloaded${NC}" # Enable service systemctl enable chatbot.service echo -e "${GREEN}✓ Service enabled (will start on boot)${NC}" # Ask if user wants to start now echo "" read -p "Do you want to start the service now? (y/n) " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then systemctl start chatbot.service sleep 2 systemctl status chatbot.service echo "" echo -e "${GREEN}✓ Service started successfully${NC}" echo -e "${GREEN}✓ Access chatbot at: http://0.0.0.0:7860${NC}" fi echo "" echo -e "${GREEN}=== Installation Complete ===${NC}" echo "" echo "Useful commands:" echo " sudo systemctl start chatbot # Start service" echo " sudo systemctl stop chatbot # Stop service" echo " sudo systemctl restart chatbot # Restart service" echo " sudo systemctl status chatbot # Check status" echo " sudo journalctl -u chatbot -f # View live logs" echo " tail -f /var/log/chatbot.log # View application logs" echo ""