deploy_toolbox.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #!/bin/bash
  2. set -e
  3. # ===== 配置部分 =====
  4. APP_NAME="fwh-toolbox-server"
  5. APP_DIR="/var/www/fwh-toolbox/server"
  6. APP_PATH="$APP_DIR/$APP_NAME"
  7. SERVICE_FILE="/etc/systemd/system/$APP_NAME.service"
  8. # ===== 编译程序 =====
  9. echo "🚀 Building $APP_NAME..."
  10. GOOS=linux GOARCH=amd64 go build -o $APP_NAME ./
  11. # ===== 复制程序到目标路径 =====
  12. echo "📦 Copying binary to $APP_PATH..."
  13. sudo mv $APP_NAME $APP_PATH
  14. sudo chmod +x $APP_PATH
  15. # ===== 安装 systemd 服务(如果不存在) =====
  16. if [ ! -f "$SERVICE_FILE" ]; then
  17. echo "📝 Creating systemd service: $SERVICE_FILE"
  18. sudo tee $SERVICE_FILE > /dev/null <<EOF
  19. [Unit]
  20. Description=$APP_NAME service
  21. After=network.target docker.service
  22. Requires=docker.service
  23. [Service]
  24. ExecStartPre=/bin/bash -c '[ $(awk -F. "{print \$1}" /proc/uptime) -lt 60 ] && sleep 15 || true'
  25. ExecStart=$APP_PATH
  26. Restart=always
  27. User=root
  28. WorkingDirectory=$APP_DIR
  29. CPUQuota=200%
  30. MemoryLimit=2G
  31. [Install]
  32. WantedBy=multi-user.target
  33. EOF
  34. echo "✅ Service file created."
  35. sudo systemctl daemon-reload
  36. sudo systemctl enable $APP_NAME
  37. fi
  38. # ===== 启动或重启服务 =====
  39. if systemctl is-active --quiet $APP_NAME; then
  40. echo "🔄 Restarting $APP_NAME service..."
  41. sudo systemctl restart $APP_NAME
  42. else
  43. echo "▶️ Starting $APP_NAME service..."
  44. sudo systemctl start $APP_NAME
  45. fi
  46. # ===== 状态检查 =====
  47. sudo systemctl status $APP_NAME --no-pager