#!/bin/bash set -euo pipefail #################### 基本变量 #################### Server_Dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" Service_Name="clash-for-linux" Service_User="root" Service_Group="root" Unit_Path="/etc/systemd/system/${Service_Name}.service" Env_File="$Server_Dir/temp/clash-for-linux.sh" #################### 权限检查 #################### if [ "$(id -u)" -ne 0 ]; then echo -e "[ERROR] 需要 root 权限来安装 systemd 单元" exit 1 fi #################### 目录初始化 #################### 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" </dev/null 2>&1 || true 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"