fix(utils): now it can be successfully restarted when deployed as systemd service

- Add `getSystemdServiceName` function to get systemd service name.
- When detected as systemd sevice, use `systemctl restart` to restart the program then exit.
This commit is contained in:
2026-06-04 12:05:09 +08:00
parent 682aa1abb9
commit 5e9bdf02f9
4 changed files with 85 additions and 2 deletions
+18
View File
@@ -671,3 +671,21 @@ func RestartInitDService(instanceName string) error {
}
return nil
}
func GetSystemdServiceName() (string, error) {
data, err := os.ReadFile("/proc/self/cgroup")
if err == nil {
for _, line := range strings.Split(string(data), "\n") {
if !strings.Contains(line, ".service") {
continue
}
for _, part := range strings.Split(line, "/") {
part = strings.TrimSpace(part)
if strings.HasSuffix(part, ".service") {
return part, nil
}
}
}
}
return "", fmt.Errorf("could not determine systemd service name from cgroup")
}