alex4cip Claude commited on
Commit
1a8caac
Β·
1 Parent(s): 27061cd

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>

Files changed (3) hide show
  1. README.md +147 -1
  2. chatbot.service +17 -0
  3. install-service.sh +121 -0
README.md CHANGED
@@ -247,13 +247,159 @@ echo "HF_TOKEN=your_hugging_face_token" > .env
247
  python app.py
248
  ```
249
 
250
- λΈŒλΌμš°μ €μ—μ„œ http://localhost:7860 접속
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
chatbot.service ADDED
@@ -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
install-service.sh ADDED
@@ -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 ""