| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- #!/bin/bash
- set -e
- # ===== 配置部分 =====
- APP_NAME="fwh-toolbox-server" # 服务名称
- APP_DIR="/var/www/fwh-toolbox/server" # 二进制运行路径
- APP_PATH="$APP_DIR/$APP_NAME" # 二进制安装路径
- SERVICE_FILE="/etc/systemd/system/$APP_NAME.service"
- # ===== 编译程序 =====
- echo "🚀 Building Go program..."
- GOOS=linux GOARCH=amd64 go build -o $APP_NAME ./ # 按需修改 ./ 为你的项目路径
- # ===== 复制程序到目标路径 =====
- echo "📦 Copying binary to $APP_PATH..."
- sudo mv $APP_NAME $APP_PATH
- sudo chmod +x $APP_PATH
- # ===== 安装 systemd 服务(如果不存在) =====
- if [ ! -f "$SERVICE_FILE" ]; then
- echo "📝 Creating systemd service: $SERVICE_FILE"
- sudo tee $SERVICE_FILE > /dev/null <<EOF
- [Unit]
- Description=$APP_NAME service
- After=network.target
- [Service]
- ExecStart=$APP_PATH
- Restart=always
- User=root
- WorkingDirectory=$APP_DIR
- [Install]
- WantedBy=multi-user.target
- EOF
- echo "✅ Service file created."
- sudo systemctl daemon-reload
- sudo systemctl enable $APP_NAME
- fi
- # ===== 启动或重启服务 =====
- if systemctl is-active --quiet $APP_NAME; then
- echo "🔄 Restarting $APP_NAME service..."
- sudo systemctl restart $APP_NAME
- else
- echo "▶️ Starting $APP_NAME service..."
- sudo systemctl start $APP_NAME
- fi
- # ===== 状态检查 =====
- sudo systemctl status $APP_NAME --no-pager
|