go-platform/scripts/install-systemd-service.sh

169 lines
4.4 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# ========================================
# 安装 systemd 服务脚本
# 用途:配置 Go API 为 systemd 服务
# ========================================
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 配置
SERVICE_NAME="go-api"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SERVICE_TEMPLATE="${SCRIPT_DIR}/${SERVICE_NAME}.service"
WORK_DIR="/www/wwwroot/api.yunzer.cn"
MAIN_FILE="${WORK_DIR}/main.go"
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}安装 Go API systemd 服务${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
# 检查是否为 root
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}错误:请使用 root 用户运行此脚本${NC}"
echo "使用方法: sudo bash $0"
exit 1
fi
# 检查工作目录
if [ ! -d "$WORK_DIR" ]; then
echo -e "${RED}错误:工作目录不存在: $WORK_DIR${NC}"
exit 1
fi
# 检查 main.go
if [ ! -f "$MAIN_FILE" ]; then
echo -e "${RED}错误:找不到 main.go: $MAIN_FILE${NC}"
exit 1
fi
# 检查 Go 是否安装
if ! command -v go &> /dev/null; then
echo -e "${RED}错误Go 未安装或不在 PATH 中${NC}"
exit 1
fi
GO_PATH=$(which go)
echo -e "${GREEN}✓ Go 路径: $GO_PATH${NC}"
# 停止现有服务
echo -e "${YELLOW}1. 停止现有服务...${NC}"
if systemctl is-active --quiet "$SERVICE_NAME"; then
systemctl stop "$SERVICE_NAME"
echo -e "${GREEN} ✓ 已停止现有服务${NC}"
else
echo -e "${YELLOW} - 服务未运行${NC}"
fi
# 停止可能的手动启动进程
echo -e "${YELLOW}2. 清理手动启动的进程...${NC}"
if pgrep -f "go run main.go" > /dev/null; then
pkill -f "go run main.go"
echo -e "${GREEN} ✓ 已清理手动启动的进程${NC}"
else
echo -e "${YELLOW} - 无手动启动的进程${NC}"
fi
# 创建服务文件
echo -e "${YELLOW}3. 创建 systemd 服务文件...${NC}"
cat > "$SERVICE_FILE" << EOF
[Unit]
Description=Go API Server
After=network.target mysql.service
Wants=mysql.service
[Service]
Type=simple
User=root
Group=root
WorkingDirectory=$WORK_DIR
ExecStart=$GO_PATH run main.go
Restart=always
RestartSec=5
StandardOutput=append:$WORK_DIR/go.log
StandardError=append:$WORK_DIR/go.log
# 环境变量
Environment="PATH=$GO_PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# 资源限制
LimitNOFILE=65535
LimitNPROC=65535
# 安全设置
PrivateTmp=true
NoNewPrivileges=false
[Install]
WantedBy=multi-user.target
EOF
echo -e "${GREEN} ✓ 服务文件已创建: $SERVICE_FILE${NC}"
# 重载 systemd
echo -e "${YELLOW}4. 重载 systemd 配置...${NC}"
systemctl daemon-reload
echo -e "${GREEN} ✓ systemd 配置已重载${NC}"
# 启动服务
echo -e "${YELLOW}5. 启动服务...${NC}"
systemctl start "$SERVICE_NAME"
sleep 2
# 检查服务状态
if systemctl is-active --quiet "$SERVICE_NAME"; then
echo -e "${GREEN} ✓ 服务启动成功${NC}"
else
echo -e "${RED} ✗ 服务启动失败${NC}"
echo ""
echo -e "${YELLOW}查看错误日志:${NC}"
journalctl -u "$SERVICE_NAME" -n 20 --no-pager
exit 1
fi
# 启用开机自启
echo -e "${YELLOW}6. 启用开机自启...${NC}"
systemctl enable "$SERVICE_NAME"
echo -e "${GREEN} ✓ 已启用开机自启${NC}"
# 显示服务状态
echo ""
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE}安装完成!${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
echo -e "${GREEN}服务信息:${NC}"
echo -e " 服务名称: $SERVICE_NAME"
echo -e " 工作目录: $WORK_DIR"
echo -e " 日志文件: $WORK_DIR/go.log"
echo -e " 配置文件: $SERVICE_FILE"
echo ""
echo -e "${GREEN}常用命令:${NC}"
echo -e " 启动服务: systemctl start $SERVICE_NAME"
echo -e " 停止服务: systemctl stop $SERVICE_NAME"
echo -e " 重启服务: systemctl restart $SERVICE_NAME"
echo -e " 查看状态: systemctl status $SERVICE_NAME"
echo -e " 查看日志: journalctl -u $SERVICE_NAME -f"
echo -e " 查看文件日志: tail -f $WORK_DIR/go.log"
echo ""
echo -e "${YELLOW}当前服务状态:${NC}"
systemctl status "$SERVICE_NAME" --no-pager -l
echo ""
echo -e "${YELLOW}最近日志(最后 10 行):${NC}"
if [ -f "$WORK_DIR/go.log" ]; then
tail -n 10 "$WORK_DIR/go.log"
else
echo " 日志文件尚未创建"
fi
echo ""