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