Spaces:
Sleeping
feat: Add systemd service for automatic startup on Linux
Browse files- Add chatbot.service: systemd service configuration template
- Add install-service.sh: automated installation script with:
- Automatic user and path detection
- Log file creation and permission setup
- Service enablement and optional start
- Color-coded output and error handling
- Update README.md: comprehensive systemd service documentation
- Installation instructions with auto-install script
- Service management commands reference
- Manual configuration guide for advanced users
- Troubleshooting section for common issues
- Service removal instructions
Features:
- Auto-detect current user and working directory
- Create and configure log files with proper permissions
- Enable automatic startup on system boot
- Restart on failure with 10-second delay
- Comprehensive logging to /var/log/chatbot.log and chatbot-error.log
Usage: sudo ./install-service.sh
π€ Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
- README.md +147 -1
- chatbot.service +17 -0
- install-service.sh +121 -0
|
@@ -247,13 +247,159 @@ echo "HF_TOKEN=your_hugging_face_token" > .env
|
|
| 247 |
python app.py
|
| 248 |
```
|
| 249 |
|
| 250 |
-
λΈλΌμ°μ μμ http://
|
| 251 |
|
| 252 |
**μ°Έκ³ **:
|
| 253 |
- λ‘컬μ CPU/GPU μλ κ°μ§
|
| 254 |
- GPU κΆμ₯ (CUDA νμ)
|
| 255 |
- 첫 μ€ν μ λͺ¨λΈ λ€μ΄λ‘λ (μκ° μμ)
|
| 256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
## π οΈ κΈ°μ μ€ν
|
| 258 |
|
| 259 |
- **νλ μμν¬**: Gradio 5.49.1
|
|
|
|
| 247 |
python app.py
|
| 248 |
```
|
| 249 |
|
| 250 |
+
λΈλΌμ°μ μμ http://0.0.0.0:7860 μ μ (λλ http://localhost:7860)
|
| 251 |
|
| 252 |
**μ°Έκ³ **:
|
| 253 |
- λ‘컬μ CPU/GPU μλ κ°μ§
|
| 254 |
- GPU κΆμ₯ (CUDA νμ)
|
| 255 |
- 첫 μ€ν μ λͺ¨λΈ λ€μ΄λ‘λ (μκ° μμ)
|
| 256 |
|
| 257 |
+
### 리λ
μ€ μμ€ν
μλΉμ€λ‘ μ€μΉ (μλ μμ)
|
| 258 |
+
|
| 259 |
+
μλ² λΆν
μ μ±λ΄μ μλμΌλ‘ μ€ννλ €λ©΄ systemd μλΉμ€λ‘ μ€μΉν μ μμ΅λλ€.
|
| 260 |
+
|
| 261 |
+
#### 1. μ€μΉ μ€ν¬λ¦½νΈ μ€ν
|
| 262 |
+
|
| 263 |
+
```bash
|
| 264 |
+
# νλ‘μ νΈ λλ ν 리μμ μ€ν
|
| 265 |
+
sudo ./install-service.sh
|
| 266 |
+
```
|
| 267 |
+
|
| 268 |
+
μ€μΉ μ€ν¬λ¦½νΈκ° μλμΌλ‘:
|
| 269 |
+
- νμ¬ μ¬μ©μμ λλ ν 리 κ²½λ‘λ₯Ό κ°μ§
|
| 270 |
+
- systemd μλΉμ€ νμΌμ `/etc/systemd/system/chatbot.service`μ μ€μΉ
|
| 271 |
+
- λ‘κ·Έ νμΌ μμ± (`/var/log/chatbot.log`, `/var/log/chatbot-error.log`)
|
| 272 |
+
- λΆν
μ μλ μμ νμ±ν
|
| 273 |
+
- μλΉμ€ μ¦μ μμ μ¬λΆ νμΈ
|
| 274 |
+
|
| 275 |
+
#### 2. μλΉμ€ κ΄λ¦¬ λͺ
λ Ήμ΄
|
| 276 |
+
|
| 277 |
+
```bash
|
| 278 |
+
# μλΉμ€ μμ
|
| 279 |
+
sudo systemctl start chatbot
|
| 280 |
+
|
| 281 |
+
# μλΉμ€ μ€μ§
|
| 282 |
+
sudo systemctl stop chatbot
|
| 283 |
+
|
| 284 |
+
# μλΉμ€ μ¬μμ
|
| 285 |
+
sudo systemctl restart chatbot
|
| 286 |
+
|
| 287 |
+
# μλΉμ€ μν νμΈ
|
| 288 |
+
sudo systemctl status chatbot
|
| 289 |
+
|
| 290 |
+
# μ€μκ° λ‘κ·Έ 보기
|
| 291 |
+
sudo journalctl -u chatbot -f
|
| 292 |
+
|
| 293 |
+
# μ ν리μΌμ΄μ
λ‘κ·Έ 보기
|
| 294 |
+
tail -f /var/log/chatbot.log
|
| 295 |
+
|
| 296 |
+
# μλ¬ λ‘κ·Έ 보기
|
| 297 |
+
tail -f /var/log/chatbot-error.log
|
| 298 |
+
|
| 299 |
+
# λΆν
μ μλ μμ νμ±ν
|
| 300 |
+
sudo systemctl enable chatbot
|
| 301 |
+
|
| 302 |
+
# λΆν
μ μλ μμ λΉνμ±ν
|
| 303 |
+
sudo systemctl disable chatbot
|
| 304 |
+
```
|
| 305 |
+
|
| 306 |
+
#### 3. μλΉμ€ μμ
|
| 307 |
+
|
| 308 |
+
μλΉμ€λ₯Ό μμ ν μ κ±°νλ €λ©΄:
|
| 309 |
+
|
| 310 |
+
```bash
|
| 311 |
+
# μλΉμ€ μ€μ§ λ° λΉνμ±ν
|
| 312 |
+
sudo systemctl stop chatbot
|
| 313 |
+
sudo systemctl disable chatbot
|
| 314 |
+
|
| 315 |
+
# μλΉμ€ νμΌ μμ
|
| 316 |
+
sudo rm /etc/systemd/system/chatbot.service
|
| 317 |
+
|
| 318 |
+
# systemd λ°λͺ¬ μ¬λ‘λ
|
| 319 |
+
sudo systemctl daemon-reload
|
| 320 |
+
|
| 321 |
+
# λ‘κ·Έ νμΌ μμ (μ νμ¬ν)
|
| 322 |
+
sudo rm /var/log/chatbot.log /var/log/chatbot-error.log
|
| 323 |
+
```
|
| 324 |
+
|
| 325 |
+
#### 4. μ£Όμμ¬ν
|
| 326 |
+
|
| 327 |
+
- **κ°μνκ²½ νμ**: μλΉμ€ μ€μΉ μ μ `venv` λλ ν λ¦¬κ° μ‘΄μ¬ν΄μΌ ν©λλ€
|
| 328 |
+
- **ν¬νΈ μΆ©λ**: κΈ°μ‘΄ νλ‘μΈμ€κ° 7860 ν¬νΈλ₯Ό μ¬μ© μ€μ΄λ©΄ μλΉμ€κ° μμλμ§ μμ΅λλ€
|
| 329 |
+
- **κΆν**: μ€μΉ μ€ν¬λ¦½νΈλ λ°λμ `sudo`λ‘ μ€νν΄μΌ ν©λλ€
|
| 330 |
+
- **μ¬μμ**: μ± μ½λ λ³κ²½ νμλ `sudo systemctl restart chatbot` μ€ν νμ
|
| 331 |
+
- **λ‘κ·Έ νμΈ**: λ¬Έμ λ°μ μ λ‘κ·Έ νμΌμ λ¨Όμ νμΈνμΈμ
|
| 332 |
+
|
| 333 |
+
#### 5. μλ μλΉμ€ μ€μ (κ³ κΈ)
|
| 334 |
+
|
| 335 |
+
μλ μ€μΉ μ€ν¬λ¦½νΈ λμ μλμΌλ‘ μ€μ νλ €λ©΄:
|
| 336 |
+
|
| 337 |
+
```bash
|
| 338 |
+
# 1. chatbot.service νμΌ νΈμ§
|
| 339 |
+
sudo nano /etc/systemd/system/chatbot.service
|
| 340 |
+
|
| 341 |
+
# 2. λ€μ λ΄μ© μ
λ ₯ (κ²½λ‘μ μ¬μ©μλͺ
μμ νμ)
|
| 342 |
+
[Unit]
|
| 343 |
+
Description=Multi-Model Chatbot Gradio Service
|
| 344 |
+
After=network.target
|
| 345 |
+
|
| 346 |
+
[Service]
|
| 347 |
+
Type=simple
|
| 348 |
+
User=YOUR_USERNAME
|
| 349 |
+
WorkingDirectory=/path/to/simple-chatbot-gradio
|
| 350 |
+
Environment="PATH=/path/to/simple-chatbot-gradio/venv/bin:/usr/bin:/bin"
|
| 351 |
+
ExecStart=/path/to/simple-chatbot-gradio/venv/bin/python app.py
|
| 352 |
+
Restart=on-failure
|
| 353 |
+
RestartSec=10
|
| 354 |
+
StandardOutput=append:/var/log/chatbot.log
|
| 355 |
+
StandardError=append:/var/log/chatbot-error.log
|
| 356 |
+
|
| 357 |
+
[Install]
|
| 358 |
+
WantedBy=multi-user.target
|
| 359 |
+
|
| 360 |
+
# 3. λ‘κ·Έ νμΌ μμ±
|
| 361 |
+
sudo touch /var/log/chatbot.log /var/log/chatbot-error.log
|
| 362 |
+
sudo chown YOUR_USERNAME:YOUR_USERNAME /var/log/chatbot.log /var/log/chatbot-error.log
|
| 363 |
+
|
| 364 |
+
# 4. systemd λ°λͺ¬ μ¬λ‘λ λ° μλΉμ€ νμ±ν
|
| 365 |
+
sudo systemctl daemon-reload
|
| 366 |
+
sudo systemctl enable chatbot
|
| 367 |
+
sudo systemctl start chatbot
|
| 368 |
+
```
|
| 369 |
+
|
| 370 |
+
#### 6. νΈλ¬λΈμν
|
| 371 |
+
|
| 372 |
+
**μλΉμ€κ° μμλμ§ μλ κ²½μ°**:
|
| 373 |
+
```bash
|
| 374 |
+
# μλΉμ€ μν νμΈ
|
| 375 |
+
sudo systemctl status chatbot
|
| 376 |
+
|
| 377 |
+
# μλ¬ λ‘κ·Έ νμΈ
|
| 378 |
+
sudo journalctl -u chatbot -n 50
|
| 379 |
+
|
| 380 |
+
# μλ μ€νμΌλ‘ μλ¬ νμΈ
|
| 381 |
+
cd /path/to/simple-chatbot-gradio
|
| 382 |
+
source venv/bin/activate
|
| 383 |
+
python app.py
|
| 384 |
+
```
|
| 385 |
+
|
| 386 |
+
**ν¬νΈκ° μ΄λ―Έ μ¬μ© μ€μΈ κ²½μ°**:
|
| 387 |
+
```bash
|
| 388 |
+
# ν¬νΈ 7860μ μ¬μ©νλ νλ‘μΈμ€ νμΈ
|
| 389 |
+
sudo lsof -i :7860
|
| 390 |
+
|
| 391 |
+
# νλ‘μΈμ€ μ’
λ£ (PID νμΈ ν)
|
| 392 |
+
sudo kill -9 <PID>
|
| 393 |
+
```
|
| 394 |
+
|
| 395 |
+
**κ°μνκ²½ κ²½λ‘ λ¬Έμ **:
|
| 396 |
+
```bash
|
| 397 |
+
# κ°μνκ²½ μ¬μμ±
|
| 398 |
+
python -m venv venv
|
| 399 |
+
source venv/bin/activate
|
| 400 |
+
pip install -r requirements-local.txt
|
| 401 |
+
```
|
| 402 |
+
|
| 403 |
## π οΈ κΈ°μ μ€ν
|
| 404 |
|
| 405 |
- **νλ μμν¬**: Gradio 5.49.1
|
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[Unit]
|
| 2 |
+
Description=Multi-Model Chatbot Gradio Service
|
| 3 |
+
After=network.target
|
| 4 |
+
|
| 5 |
+
[Service]
|
| 6 |
+
Type=simple
|
| 7 |
+
User=YOUR_USERNAME
|
| 8 |
+
WorkingDirectory=/path/to/simple-chatbot-gradio
|
| 9 |
+
Environment="PATH=/path/to/simple-chatbot-gradio/venv/bin:/usr/bin:/bin"
|
| 10 |
+
ExecStart=/path/to/simple-chatbot-gradio/venv/bin/python app.py
|
| 11 |
+
Restart=on-failure
|
| 12 |
+
RestartSec=10
|
| 13 |
+
StandardOutput=append:/var/log/chatbot.log
|
| 14 |
+
StandardError=append:/var/log/chatbot-error.log
|
| 15 |
+
|
| 16 |
+
[Install]
|
| 17 |
+
WantedBy=multi-user.target
|
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# Install Multi-Model Chatbot as systemd service
|
| 3 |
+
# This script must be run with sudo privileges
|
| 4 |
+
|
| 5 |
+
set -e # Exit on error
|
| 6 |
+
|
| 7 |
+
# Colors for output
|
| 8 |
+
RED='\033[0;31m'
|
| 9 |
+
GREEN='\033[0;32m'
|
| 10 |
+
YELLOW='\033[1;33m'
|
| 11 |
+
NC='\033[0m' # No Color
|
| 12 |
+
|
| 13 |
+
# Check if running as root
|
| 14 |
+
if [ "$EUID" -ne 0 ]; then
|
| 15 |
+
echo -e "${RED}Error: This script must be run as root (use sudo)${NC}"
|
| 16 |
+
exit 1
|
| 17 |
+
fi
|
| 18 |
+
|
| 19 |
+
# Get the actual user (not root)
|
| 20 |
+
ACTUAL_USER=${SUDO_USER:-$USER}
|
| 21 |
+
if [ "$ACTUAL_USER" = "root" ]; then
|
| 22 |
+
echo -e "${RED}Error: Please run with sudo as a regular user, not as root${NC}"
|
| 23 |
+
exit 1
|
| 24 |
+
fi
|
| 25 |
+
|
| 26 |
+
# Get current directory
|
| 27 |
+
CURRENT_DIR=$(pwd)
|
| 28 |
+
SERVICE_FILE="chatbot.service"
|
| 29 |
+
|
| 30 |
+
echo -e "${GREEN}=== Multi-Model Chatbot Service Installer ===${NC}"
|
| 31 |
+
echo ""
|
| 32 |
+
|
| 33 |
+
# Check if service file exists
|
| 34 |
+
if [ ! -f "$SERVICE_FILE" ]; then
|
| 35 |
+
echo -e "${RED}Error: $SERVICE_FILE not found in current directory${NC}"
|
| 36 |
+
exit 1
|
| 37 |
+
fi
|
| 38 |
+
|
| 39 |
+
# Check if venv exists
|
| 40 |
+
if [ ! -d "venv" ]; then
|
| 41 |
+
echo -e "${YELLOW}Warning: venv directory not found${NC}"
|
| 42 |
+
echo -e "${YELLOW}Please create virtual environment first:${NC}"
|
| 43 |
+
echo " python -m venv venv"
|
| 44 |
+
echo " source venv/bin/activate"
|
| 45 |
+
echo " pip install -r requirements-local.txt"
|
| 46 |
+
exit 1
|
| 47 |
+
fi
|
| 48 |
+
|
| 49 |
+
# Check if app.py exists
|
| 50 |
+
if [ ! -f "app.py" ]; then
|
| 51 |
+
echo -e "${RED}Error: app.py not found in current directory${NC}"
|
| 52 |
+
exit 1
|
| 53 |
+
fi
|
| 54 |
+
|
| 55 |
+
# Create customized service file
|
| 56 |
+
TEMP_SERVICE="/tmp/chatbot-temp.service"
|
| 57 |
+
cp "$SERVICE_FILE" "$TEMP_SERVICE"
|
| 58 |
+
|
| 59 |
+
# Replace placeholders
|
| 60 |
+
sed -i "s|YOUR_USERNAME|$ACTUAL_USER|g" "$TEMP_SERVICE"
|
| 61 |
+
sed -i "s|/path/to/simple-chatbot-gradio|$CURRENT_DIR|g" "$TEMP_SERVICE"
|
| 62 |
+
|
| 63 |
+
echo -e "${GREEN}β Service file configured:${NC}"
|
| 64 |
+
echo " User: $ACTUAL_USER"
|
| 65 |
+
echo " Directory: $CURRENT_DIR"
|
| 66 |
+
echo ""
|
| 67 |
+
|
| 68 |
+
# Create log directory if not exists
|
| 69 |
+
LOG_DIR="/var/log"
|
| 70 |
+
if [ ! -d "$LOG_DIR" ]; then
|
| 71 |
+
mkdir -p "$LOG_DIR"
|
| 72 |
+
fi
|
| 73 |
+
|
| 74 |
+
# Set log file permissions
|
| 75 |
+
touch /var/log/chatbot.log
|
| 76 |
+
touch /var/log/chatbot-error.log
|
| 77 |
+
chown $ACTUAL_USER:$ACTUAL_USER /var/log/chatbot.log
|
| 78 |
+
chown $ACTUAL_USER:$ACTUAL_USER /var/log/chatbot-error.log
|
| 79 |
+
chmod 644 /var/log/chatbot.log
|
| 80 |
+
chmod 644 /var/log/chatbot-error.log
|
| 81 |
+
|
| 82 |
+
echo -e "${GREEN}β Log files created${NC}"
|
| 83 |
+
|
| 84 |
+
# Copy service file to systemd directory
|
| 85 |
+
cp "$TEMP_SERVICE" /etc/systemd/system/chatbot.service
|
| 86 |
+
rm "$TEMP_SERVICE"
|
| 87 |
+
|
| 88 |
+
echo -e "${GREEN}β Service file installed to /etc/systemd/system/chatbot.service${NC}"
|
| 89 |
+
|
| 90 |
+
# Reload systemd daemon
|
| 91 |
+
systemctl daemon-reload
|
| 92 |
+
echo -e "${GREEN}β Systemd daemon reloaded${NC}"
|
| 93 |
+
|
| 94 |
+
# Enable service
|
| 95 |
+
systemctl enable chatbot.service
|
| 96 |
+
echo -e "${GREEN}β Service enabled (will start on boot)${NC}"
|
| 97 |
+
|
| 98 |
+
# Ask if user wants to start now
|
| 99 |
+
echo ""
|
| 100 |
+
read -p "Do you want to start the service now? (y/n) " -n 1 -r
|
| 101 |
+
echo ""
|
| 102 |
+
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
| 103 |
+
systemctl start chatbot.service
|
| 104 |
+
sleep 2
|
| 105 |
+
systemctl status chatbot.service
|
| 106 |
+
echo ""
|
| 107 |
+
echo -e "${GREEN}β Service started successfully${NC}"
|
| 108 |
+
echo -e "${GREEN}β Access chatbot at: http://0.0.0.0:7860${NC}"
|
| 109 |
+
fi
|
| 110 |
+
|
| 111 |
+
echo ""
|
| 112 |
+
echo -e "${GREEN}=== Installation Complete ===${NC}"
|
| 113 |
+
echo ""
|
| 114 |
+
echo "Useful commands:"
|
| 115 |
+
echo " sudo systemctl start chatbot # Start service"
|
| 116 |
+
echo " sudo systemctl stop chatbot # Stop service"
|
| 117 |
+
echo " sudo systemctl restart chatbot # Restart service"
|
| 118 |
+
echo " sudo systemctl status chatbot # Check status"
|
| 119 |
+
echo " sudo journalctl -u chatbot -f # View live logs"
|
| 120 |
+
echo " tail -f /var/log/chatbot.log # View application logs"
|
| 121 |
+
echo ""
|