deploy.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/bash
  2. set -e
  3. # ====================== 配置 ======================
  4. PROJECT_DIR="/opt/openAI/project/003.PlateRecAPP"
  5. SERVICE_NAME="plate-log2mqtt"
  6. BINARY_NAME="plate_log2mqtt"
  7. CONFIG_FILE="config.ini"
  8. LOG_FILE="PlateRecApp.log"
  9. # ====================== 颜色输出 ======================
  10. RED='\033[0;31m'
  11. GREEN='\033[0;32m'
  12. YELLOW='\033[1;33m'
  13. NC='\033[0m'
  14. print_info() {
  15. echo -e "${GREEN}[INFO]${NC} $1"
  16. }
  17. print_warn() {
  18. echo -e "${YELLOW}[WARN]${NC} $1"
  19. }
  20. print_error() {
  21. echo -e "${RED}[ERROR]${NC} $1"
  22. }
  23. # ====================== 检查root权限 ======================
  24. if [ "$EUID" -ne 0 ]; then
  25. print_error "请使用root权限运行: sudo $0"
  26. exit 1
  27. fi
  28. # ====================== 检查项目目录 ======================
  29. print_info "检查项目目录: $PROJECT_DIR"
  30. if [ ! -d "$PROJECT_DIR" ]; then
  31. print_error "项目目录不存在: $PROJECT_DIR"
  32. exit 1
  33. fi
  34. cd "$PROJECT_DIR"
  35. # ====================== 检查二进制文件 ======================
  36. print_info "检查二进制文件..."
  37. if [ ! -f "$BINARY_NAME" ]; then
  38. print_error "二进制文件不存在: $PROJECT_DIR/$BINARY_NAME"
  39. exit 1
  40. fi
  41. if [ ! -x "$BINARY_NAME" ]; then
  42. print_info "设置执行权限..."
  43. chmod +x "$BINARY_NAME"
  44. fi
  45. # ====================== 检查配置文件 ======================
  46. if [ ! -f "$CONFIG_FILE" ]; then
  47. print_error "配置文件不存在: $CONFIG_FILE"
  48. exit 1
  49. fi
  50. # ====================== 设置权限 ======================
  51. print_info "设置文件权限..."
  52. chown -R root:root "$PROJECT_DIR"
  53. # ====================== 创建日志文件 ======================
  54. if [ ! -f "$LOG_FILE" ]; then
  55. print_info "创建日志文件: $LOG_FILE"
  56. touch "$LOG_FILE"
  57. chown root:root "$LOG_FILE"
  58. fi
  59. # ====================== 创建systemd服务 ======================
  60. print_info "创建systemd服务: $SERVICE_NAME"
  61. cat > "/etc/systemd/system/$SERVICE_NAME.service" << EOF
  62. [Unit]
  63. Description=Plate Recognition Log to MQTT Service
  64. After=network.target mosquitto.service
  65. Wants=network.target
  66. [Service]
  67. Type=simple
  68. User=root
  69. WorkingDirectory=$PROJECT_DIR
  70. ExecStart=$PROJECT_DIR/$BINARY_NAME
  71. Restart=always
  72. RestartSec=5
  73. StandardOutput=journal
  74. StandardError=journal
  75. SyslogIdentifier=$SERVICE_NAME
  76. # 资源限制
  77. LimitNOFILE=65536
  78. LimitNPROC=4096
  79. [Install]
  80. WantedBy=multi-user.target
  81. EOF
  82. # ====================== 创建日志轮转配置 ======================
  83. print_info "创建日志轮转配置..."
  84. cat > "/etc/logrotate.d/$SERVICE_NAME" << EOF
  85. $PROJECT_DIR/$LOG_FILE {
  86. daily
  87. rotate 30
  88. compress
  89. delaycompress
  90. missingok
  91. notifempty
  92. create 0644 root root
  93. sharedscripts
  94. postrotate
  95. systemctl reload $SERVICE_NAME > /dev/null 2>&1 || true
  96. endscript
  97. }
  98. EOF
  99. # ====================== 重载systemd ======================
  100. print_info "重载systemd配置..."
  101. systemctl daemon-reload
  102. # ====================== 停止旧服务(如果存在) ======================
  103. if systemctl is-active --quiet "$SERVICE_NAME"; then
  104. print_info "停止旧服务..."
  105. systemctl stop "$SERVICE_NAME"
  106. fi
  107. # ====================== 启用并启动服务 ======================
  108. print_info "启用服务开机自启..."
  109. systemctl enable "$SERVICE_NAME"
  110. print_info "启动服务..."
  111. systemctl start "$SERVICE_NAME"
  112. # ====================== 等待服务启动 ======================
  113. print_info "等待服务启动..."
  114. sleep 3
  115. # ====================== 检查服务状态 ======================
  116. if systemctl is-active --quiet "$SERVICE_NAME"; then
  117. print_info "服务启动成功!"
  118. echo ""
  119. print_info "=========================================="
  120. print_info " 部署成功!"
  121. print_info "=========================================="
  122. print_info " 服务名称: $SERVICE_NAME"
  123. print_info " 程序路径: $PROJECT_DIR/$BINARY_NAME"
  124. print_info " 配置文件: $PROJECT_DIR/$CONFIG_FILE"
  125. print_info " 日志文件: $PROJECT_DIR/$LOG_FILE"
  126. print_info ""
  127. print_info " 常用命令:"
  128. print_info " 查看状态: systemctl status $SERVICE_NAME"
  129. print_info " 查看日志: journalctl -u $SERVICE_NAME -f"
  130. print_info " 停止服务: systemctl stop $SERVICE_NAME"
  131. print_info " 启动服务: systemctl start $SERVICE_NAME"
  132. print_info " 重启服务: systemctl restart $SERVICE_NAME"
  133. print_info "=========================================="
  134. else
  135. print_error "服务启动失败"
  136. print_error "查看日志: journalctl -u $SERVICE_NAME -n 50"
  137. exit 1
  138. fi