避免误杀systemd服务进程

This commit is contained in:
hy 2026-04-23 13:14:39 +08:00
parent 04dc303730
commit aa3259583f

View File

@ -142,6 +142,22 @@ has_full_systemd_suite() {
return 0
}
systemd_main_pid() {
local name="$1"
local main_pid
if ! has_systemd_unit "$name"; then
return 1
fi
main_pid="$(systemctl show --property MainPID --value "$(service_unit_name "$name")" 2>/dev/null || true)"
if [ -z "${main_pid:-}" ] || [ "$main_pid" = "0" ]; then
return 1
fi
echo "$main_pid"
}
pid_matches_project() {
local name="$1"
local pid="$2"
@ -231,8 +247,15 @@ stop_legacy_process() {
local name="$1"
local pid
local pid_file
local managed_pid
pid_file="$(service_pid_file "$name")"
pid="$(read_pid "$name" || true)"
managed_pid="$(systemd_main_pid "$name" || true)"
if [ -n "${pid:-}" ] && [ -n "${managed_pid:-}" ] && [ "$pid" = "$managed_pid" ]; then
rm -f "$pid_file"
return 0
fi
if [ -n "${pid:-}" ] && kill -0 "$pid" >/dev/null 2>&1; then
kill "$pid" >/dev/null 2>&1 || true