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:
+18
@@ -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")
|
||||
}
|
||||
Reference in New Issue
Block a user