scripts\install_systemd.sh shutdown.sh start.sh

This commit is contained in:
wnlen
2026-01-15 13:49:23 +08:00
parent 0e1bf1d230
commit f8b6c8cea6
3 changed files with 39 additions and 22 deletions

View File

@ -51,20 +51,17 @@ chown -R "$Service_User:$Service_Group" \
cat >"$Unit_Path"<<EOF cat >"$Unit_Path"<<EOF
[Unit] [Unit]
Description=Clash for Linux Description=Clash for Linux
After=network.target After=network-online.target
Wants=network.target Wants=network-online.target
[Service] [Service]
Type=forking Type=simple
WorkingDirectory=$Server_Dir WorkingDirectory=$Server_Dir
# 启动 / 停止 # 启动 / 停止
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
# PID 管理
PIDFile=$PID_FILE
# 失败策略 # 失败策略
Restart=on-failure Restart=on-failure
RestartSec=5 RestartSec=5

View File

@ -7,12 +7,14 @@ Temp_Dir="$Server_Dir/temp"
Conf_Dir="$Server_Dir/conf" Conf_Dir="$Server_Dir/conf"
PID_FILE="$Temp_Dir/clash.pid" PID_FILE="$Temp_Dir/clash.pid"
mkdir -p "$Temp_Dir"
# 1) 优先按 PID_FILE 停 # 1) 优先按 PID_FILE 停
if [ -f "$PID_FILE" ]; then if [ -f "$PID_FILE" ]; then
PID="$(cat "$PID_FILE" 2>/dev/null || true)" PID="$(cat "$PID_FILE" 2>/dev/null || true)"
if [ -n "${PID:-}" ] && kill -0 "$PID" 2>/dev/null; then if [ -n "${PID:-}" ] && kill -0 "$PID" 2>/dev/null; then
kill "$PID" 2>/dev/null || true kill "$PID" 2>/dev/null || true
for _ in {1..5}; do for _ in {1..8}; do
sleep 1 sleep 1
if ! kill -0 "$PID" 2>/dev/null; then if ! kill -0 "$PID" 2>/dev/null; then
break break
@ -29,7 +31,7 @@ else
PIDS="$(pgrep -f " -d ${Conf_Dir}(\s|$)" || true)" PIDS="$(pgrep -f " -d ${Conf_Dir}(\s|$)" || true)"
if [ -n "${PIDS:-}" ]; then if [ -n "${PIDS:-}" ]; then
kill $PIDS 2>/dev/null || true kill $PIDS 2>/dev/null || true
for _ in {1..5}; do for _ in {1..8}; do
sleep 1 sleep 1
if ! pgrep -f " -d ${Conf_Dir}(\s|$)" >/dev/null 2>&1; then if ! pgrep -f " -d ${Conf_Dir}(\s|$)" >/dev/null 2>&1; then
break break
@ -57,4 +59,4 @@ if [ "$Env_File" != "off" ] && [ "$Env_File" != "disabled" ]; then
fi fi
fi fi
echo -e "\n服务关闭成功。若当前终端已开启代理请执行proxy_off\n" echo -e "\n服务关闭成功。若当前终端已开启代理请执行proxy_off\n"

View File

@ -18,7 +18,7 @@ Server_Dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# 加载.env变量文件 # 加载.env变量文件
# shellcheck disable=SC1090 # shellcheck disable=SC1090
source "$Server_Dir/.env" [ -f "$Server_Dir/.env" ] && source "$Server_Dir/.env"
# systemd 模式开关(必须在 set -u 下安全) # systemd 模式开关(必须在 set -u 下安全)
SYSTEMD_MODE="${SYSTEMD_MODE:-false}" SYSTEMD_MODE="${SYSTEMD_MODE:-false}"
@ -40,17 +40,28 @@ mkdir -p "$Conf_Dir" "$Temp_Dir" "$Log_Dir"
PID_FILE="${CLASH_PID_FILE:-$Temp_Dir/clash.pid}" PID_FILE="${CLASH_PID_FILE:-$Temp_Dir/clash.pid}"
is_running() {
if [ -f "$PID_FILE" ]; then
local pid
pid="$(cat "$PID_FILE" 2>/dev/null || true)"
if [ -n "${pid:-}" ] && kill -0 "$pid" 2>/dev/null; then
return 0
fi
fi
return 1
}
if is_running; then
echo -e "\n[OK] Clash 已在运行 (pid=$(cat "$PID_FILE")),跳过重复启动\n"
exit 0
fi
# 将 CLASH_URL 变量的值赋给 URL 变量,并检查 CLASH_URL 是否为空 # 将 CLASH_URL 变量的值赋给 URL 变量,并检查 CLASH_URL 是否为空
# systemd 模式:允许为空(用兜底配置启动) # systemd 模式:允许为空(用兜底配置启动)
if [ "${SYSTEMD_MODE}" = "true" ]; then if [ "$SYSTEMD_MODE" = "true" ]; then
URL="${CLASH_URL:-}" URL="${CLASH_URL:-}"
else else
SYSTEMD_MODE="${SYSTEMD_MODE:-false}" URL="${CLASH_URL:?Error: CLASH_URL variable is not set or empty}"
if [ "$SYSTEMD_MODE" = "true" ]; then
URL="${CLASH_URL:-}"
else
URL=${CLASH_URL:?Error: CLASH_URL variable is not set or empty}
fi
fi fi
# 获取 CLASH_SECRET 值:优先 .env其次读取旧 config占位符视为无效最后生成随机值 # 获取 CLASH_SECRET 值:优先 .env其次读取旧 config占位符视为无效最后生成随机值
@ -397,11 +408,18 @@ Clash_Bin="$(resolve_clash_bin "$Server_Dir" "$CpuArch")"
ReturnStatus=$? ReturnStatus=$?
if [ "$ReturnStatus" -eq 0 ]; then if [ "$ReturnStatus" -eq 0 ]; then
nohup "$Clash_Bin" -d "$Conf_Dir" &> "$Log_Dir/clash.log" & if [ "${SYSTEMD_MODE:-false}" = "true" ]; then
PID=$! echo "[INFO] SYSTEMD_MODE=true前台启动交给 systemd 监管"
ReturnStatus=$? # systemd 前台:让 systemd 直接跟踪 clash 进程
if [ "$ReturnStatus" -eq 0 ]; then exec "$Clash_Bin" -d "$Conf_Dir"
echo "$PID" > "$PID_FILE" else
echo "[INFO] 后台启动 (nohup)"
nohup "$Clash_Bin" -d "$Conf_Dir" >>"$Log_Dir/clash.log" 2>&1 &
PID=$!
ReturnStatus=$?
if [ "$ReturnStatus" -eq 0 ]; then
echo "$PID" > "$PID_FILE"
fi
fi fi
fi fi