Update install_systemd.sh

This commit is contained in:
wnlen
2026-03-16 21:56:32 +08:00
parent 4ea89a8e8f
commit 63755f5e05

View File

@ -10,47 +10,63 @@ Service_User="root"
Service_Group="root"
Unit_Path="/etc/systemd/system/${Service_Name}.service"
PID_FILE="$Server_Dir/temp/clash.pid"
Env_File="$Server_Dir/temp/clash-for-linux.sh"
#################### 权限检查 ####################
if [ "$(id -u)" -ne 0 ]; then
echo -e "\033[31m[ERROR] 需要 root 权限来安装 systemd 单元\033[0m"
echo -e "[ERROR] 需要 root 权限来安装 systemd 单元"
exit 1
fi
#################### 目录初始化 ####################
install -d -m 0755 \
"$Server_Dir/conf" \
"$Server_Dir/logs" \
"$Server_Dir/temp"
install -d -m 0755 "$Server_Dir/conf" "$Server_Dir/logs" "$Server_Dir/temp"
# 预创建 env 文件,避免 systemd 因路径不存在报错
: > "$Env_File"
chmod 0644 "$Env_File"
#################### 生成 systemd Unit ####################
cat >"$Unit_Path" <<EOF
[Unit]
Description=Clash for Linux
After=network-online.target
Description=Clash for Linux (Mihomo)
Documentation=https://github.com/wnlen/clash-for-linux
After=network-online.target nss-lookup.target
Wants=network-online.target
[Service]
Type=simple
User=$Service_User
Group=$Service_Group
WorkingDirectory=$Server_Dir
# 启动 / 停止
# 启动环境
Environment=SYSTEMD_MODE=true
Environment=CLASH_ENV_FILE=$Env_File
Environment=HOME=/root
# 主进程必须由 start.sh 最后一跳 exec 成 mihomo/clash
ExecStart=/bin/bash $Server_Dir/start.sh
ExecStop=/bin/bash $Server_Dir/shutdown.sh
ExecReload=/bin/kill -HUP \$MAINPID
# 失败策略
Restart=on-failure
RestartSec=5
# 常驻策略:即使上层脚本正常退出,也要由 systemd 拉回
Restart=always
RestartSec=5s
StartLimitIntervalSec=0
# 停止与日志
KillMode=mixed
TimeoutStartSec=120
TimeoutStopSec=30
StandardOutput=journal
StandardError=journal
# 环境变量
Environment=SYSTEMD_MODE=true
Environment=CLASH_ENV_FILE=$Server_Dir/temp/clash-for-linux.sh
# 安全与文件权限
UMask=0022
NoNewPrivileges=false
[Install]
WantedBy=multi-user.target
@ -59,7 +75,10 @@ EOF
#################### 刷新 systemd ####################
systemctl daemon-reload
systemctl enable "$Service_Name".service >/dev/null 2>&1 || true
echo -e "\033[32m[OK] 已生成 systemd 单元: ${Unit_Path}\033[0m"
echo -e "可执行以下命令启动服务:"
echo -e " sudo systemctl enable --now ${Service_Name}.service"
echo -e "[OK] 已生成 systemd 单元: ${Unit_Path}"
echo -e "已启用开机自启,可执行以下命令启动服务:"
echo -e " systemctl restart ${Service_Name}.service"
echo -e "查看状态:"
echo -e " systemctl status ${Service_Name}.service -l --no-pager"