Files
linux-bash/mengyamonitor/install_mengyamonitor.sh
2026-02-17 17:28:37 +08:00

144 lines
3.5 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.
#!/usr/bin/env bash
set -euo pipefail
# MengyaMonitor 一键安装脚本
# 用法示例curl -fsSL "https://pan.shumengya.top/d/scripts/mengyamonitor/install_mengyamonitor.sh" | sudo bash
# 目录结构假设:
# BASE_URL/linux_amd64/mengyamonitor-backend
# BASE_URL/linux_arm64/mengyamonitor-backend
BASE_URL="https://pan.shumengya.top/d/scripts/mengyamonitor"
INSTALL_DIR="/shumengya/bin/mengyamonitor"
SERVICE_NAME="smy-mengyamonitor"
BINARY_NAME="mengyamonitor-backend"
log() { printf '[MengyaMonitor-安装] %s\n' "$*" >&2; }
fail() { log "错误: $*" >&2; exit 1; }
# 进度条函数
show_progress() {
local pid=$1
local text=$2
local delay=0.1
local spin='-\|/'
printf "[MengyaMonitor-安装] %s... " "$text" >&2
while ps -p "$pid" > /dev/null 2>&1; do
local temp=${spin#?}
printf "\b%c" "$spin" >&2
local spin=$temp${spin%"$temp"}
sleep $delay
done
printf "\b完成\n" >&2
}
require_root() {
if [ "${EUID:-$(id -u)}" -ne 0 ]; then
fail "请使用 root 权限运行 (sudo bash install_mengyamonitor.sh)"
fi
}
detect_arch() {
local machine
machine=$(uname -m)
case "$machine" in
x86_64|amd64) echo "linux_amd64" ;;
aarch64|arm64) echo "linux_arm64" ;;
*) fail "不支持的架构: $machine" ;;
esac
}
download_and_install() {
local arch=$1
local tmp_dir
tmp_dir=$(mktemp -d)
trap 'if [ -n "${tmp_dir:-}" ] && [ -d "$tmp_dir" ]; then rm -rf "$tmp_dir"; fi' EXIT
mkdir -p "$INSTALL_DIR"
local bin_url="${BASE_URL}/${arch}/${BINARY_NAME}"
log "正在处理 ${BINARY_NAME} ..."
# 停止旧服务
systemctl stop "${SERVICE_NAME}.service" 2>/dev/null || true
# 强制清理可能残留的进程 (避免 Text file busy)
pids=$(pgrep -f "$BINARY_NAME" || true)
if [ -n "$pids" ]; then
log "清理残留进程..."
kill -9 $pids 2>/dev/null || true
fi
# 优先检测当前目录下是否有二进制文件(本地安装模式)
if [ -f "./${BINARY_NAME}" ]; then
log "发现本地文件 ./${BINARY_NAME},跳过下载..."
cp "./${BINARY_NAME}" "$tmp_dir/$BINARY_NAME"
else
# 下载二进制
curl -fsSL "$bin_url" -o "$tmp_dir/$BINARY_NAME" &
show_progress $! "下载二进制文件"
[ -f "$tmp_dir/$BINARY_NAME" ] || fail "下载失败"
fi
# 安装文件
cp "$tmp_dir/$BINARY_NAME" "$INSTALL_DIR/$BINARY_NAME"
chmod +x "$INSTALL_DIR/$BINARY_NAME"
# 配置服务
write_service
# 启动服务
systemctl daemon-reload
systemctl enable "${SERVICE_NAME}.service"
systemctl restart "${SERVICE_NAME}.service"
sleep 2
if systemctl is-active --quiet "${SERVICE_NAME}.service"; then
log "服务启动成功"
else
log "警告: 服务启动失败,查看日志:"
journalctl -u "${SERVICE_NAME}.service" --no-pager -n 10
fi
}
write_service() {
local service_file="/etc/systemd/system/${SERVICE_NAME}.service"
cat > "$service_file" <<EOF
[Unit]
Description=MengyaMonitor Backend Service
After=network.target
[Service]
Type=simple
WorkingDirectory=$INSTALL_DIR
ExecStart=$INSTALL_DIR/$BINARY_NAME
Restart=on-failure
RestartSec=5
StandardOutput=append:/var/log/mengyamonitor.log
StandardError=append:/var/log/mengyamonitor.log
[Install]
WantedBy=multi-user.target
EOF
}
main() {
require_root
local arch
arch=$(detect_arch)
log "检测到架构: $arch"
download_and_install "$arch"
log "=========================================="
log "安装任务完成!"
log "安装目录: $INSTALL_DIR"
log "服务名称: $SERVICE_NAME"
log "=========================================="
}
main "$@"