| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #!/bin/bash
- set -e
- # ====================== 配置 ======================
- PROJECT_DIR="/opt/openAI/project/003.PlateRecAPP"
- SERVICE_NAME="plate-log2mqtt"
- BINARY_NAME="plate_log2mqtt"
- CONFIG_FILE="config.ini"
- LOG_FILE="PlateRecApp.log"
- # ====================== 颜色输出 ======================
- RED='\033[0;31m'
- GREEN='\033[0;32m'
- YELLOW='\033[1;33m'
- NC='\033[0m'
- print_info() {
- echo -e "${GREEN}[INFO]${NC} $1"
- }
- print_warn() {
- echo -e "${YELLOW}[WARN]${NC} $1"
- }
- print_error() {
- echo -e "${RED}[ERROR]${NC} $1"
- }
- # ====================== 检查root权限 ======================
- if [ "$EUID" -ne 0 ]; then
- print_error "请使用root权限运行: sudo $0"
- exit 1
- fi
- # ====================== 检查项目目录 ======================
- print_info "检查项目目录: $PROJECT_DIR"
- if [ ! -d "$PROJECT_DIR" ]; then
- print_error "项目目录不存在: $PROJECT_DIR"
- exit 1
- fi
- cd "$PROJECT_DIR"
- # ====================== 检查二进制文件 ======================
- print_info "检查二进制文件..."
- if [ ! -f "$BINARY_NAME" ]; then
- print_error "二进制文件不存在: $PROJECT_DIR/$BINARY_NAME"
- exit 1
- fi
- if [ ! -x "$BINARY_NAME" ]; then
- print_info "设置执行权限..."
- chmod +x "$BINARY_NAME"
- fi
- # ====================== 检查配置文件 ======================
- if [ ! -f "$CONFIG_FILE" ]; then
- print_error "配置文件不存在: $CONFIG_FILE"
- exit 1
- fi
- # ====================== 设置权限 ======================
- print_info "设置文件权限..."
- chown -R root:root "$PROJECT_DIR"
- # ====================== 创建日志文件 ======================
- if [ ! -f "$LOG_FILE" ]; then
- print_info "创建日志文件: $LOG_FILE"
- touch "$LOG_FILE"
- chown root:root "$LOG_FILE"
- fi
- # ====================== 创建systemd服务 ======================
- print_info "创建systemd服务: $SERVICE_NAME"
- cat > "/etc/systemd/system/$SERVICE_NAME.service" << EOF
- [Unit]
- Description=Plate Recognition Log to MQTT Service
- After=network.target mosquitto.service
- Wants=network.target
- [Service]
- Type=simple
- User=root
- WorkingDirectory=$PROJECT_DIR
- ExecStart=$PROJECT_DIR/$BINARY_NAME
- Restart=always
- RestartSec=5
- StandardOutput=journal
- StandardError=journal
- SyslogIdentifier=$SERVICE_NAME
- # 资源限制
- LimitNOFILE=65536
- LimitNPROC=4096
- [Install]
- WantedBy=multi-user.target
- EOF
- # ====================== 创建日志轮转配置 ======================
- print_info "创建日志轮转配置..."
- cat > "/etc/logrotate.d/$SERVICE_NAME" << EOF
- $PROJECT_DIR/$LOG_FILE {
- daily
- rotate 30
- compress
- delaycompress
- missingok
- notifempty
- create 0644 root root
- sharedscripts
- postrotate
- systemctl reload $SERVICE_NAME > /dev/null 2>&1 || true
- endscript
- }
- EOF
- # ====================== 重载systemd ======================
- print_info "重载systemd配置..."
- systemctl daemon-reload
- # ====================== 停止旧服务(如果存在) ======================
- if systemctl is-active --quiet "$SERVICE_NAME"; then
- print_info "停止旧服务..."
- systemctl stop "$SERVICE_NAME"
- fi
- # ====================== 启用并启动服务 ======================
- print_info "启用服务开机自启..."
- systemctl enable "$SERVICE_NAME"
- print_info "启动服务..."
- systemctl start "$SERVICE_NAME"
- # ====================== 等待服务启动 ======================
- print_info "等待服务启动..."
- sleep 3
- # ====================== 检查服务状态 ======================
- if systemctl is-active --quiet "$SERVICE_NAME"; then
- print_info "服务启动成功!"
- echo ""
- print_info "=========================================="
- print_info " 部署成功!"
- print_info "=========================================="
- print_info " 服务名称: $SERVICE_NAME"
- print_info " 程序路径: $PROJECT_DIR/$BINARY_NAME"
- print_info " 配置文件: $PROJECT_DIR/$CONFIG_FILE"
- print_info " 日志文件: $PROJECT_DIR/$LOG_FILE"
- print_info ""
- print_info " 常用命令:"
- print_info " 查看状态: systemctl status $SERVICE_NAME"
- print_info " 查看日志: journalctl -u $SERVICE_NAME -f"
- print_info " 停止服务: systemctl stop $SERVICE_NAME"
- print_info " 启动服务: systemctl start $SERVICE_NAME"
- print_info " 重启服务: systemctl restart $SERVICE_NAME"
- print_info "=========================================="
- else
- print_error "服务启动失败"
- print_error "查看日志: journalctl -u $SERVICE_NAME -n 50"
- exit 1
- fi
|