diff --git a/scripts/install_systemd.sh b/scripts/install_systemd.sh index 4aa67f7..d15adf5 100755 --- a/scripts/install_systemd.sh +++ b/scripts/install_systemd.sh @@ -1,51 +1,90 @@ #!/bin/bash - set -euo pipefail -Server_Dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd) +#################### 基本变量 #################### + +Server_Dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" Service_Name="clash-for-linux" + Service_User="${CLASH_SERVICE_USER:-clash}" Service_Group="${CLASH_SERVICE_GROUP:-$Service_User}" + Unit_Path="/etc/systemd/system/${Service_Name}.service" +PID_FILE="$Server_Dir/temp/clash.pid" + +#################### 权限检查 #################### if [ "$(id -u)" -ne 0 ]; then - echo -e "\033[31m[ERROR] 需要 root 权限来安装 systemd 单元\033[0m" - exit 1 + echo -e "\033[31m[ERROR] 需要 root 权限来安装 systemd 单元\033[0m" + exit 1 fi +#################### 用户 / 组 #################### + if ! getent group "$Service_Group" >/dev/null 2>&1; then - groupadd --system "$Service_Group" + groupadd --system "$Service_Group" fi if ! id "$Service_User" >/dev/null 2>&1; then - useradd --system --no-create-home --shell /usr/sbin/nologin --gid "$Service_Group" "$Service_User" + useradd \ + --system \ + --no-create-home \ + --shell /usr/sbin/nologin \ + --gid "$Service_Group" \ + "$Service_User" fi -install -d -m 0755 "$Server_Dir/conf" "$Server_Dir/logs" "$Server_Dir/temp" -chown -R "$Service_User:$Service_Group" "$Server_Dir/conf" "$Server_Dir/logs" "$Server_Dir/temp" +#################### 目录初始化 #################### + +install -d -m 0755 \ + "$Server_Dir/conf" \ + "$Server_Dir/logs" \ + "$Server_Dir/temp" + +chown -R "$Service_User:$Service_Group" \ + "$Server_Dir/conf" \ + "$Server_Dir/logs" \ + "$Server_Dir/temp" + +#################### 生成 systemd Unit #################### cat >"$Unit_Path"<