- Add `getSystemdServiceName` function to get systemd service name. - When detected as systemd sevice, use `systemctl restart` to restart the program then exit.
41 lines
842 B
Go
41 lines
842 B
Go
package utils
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"runtime"
|
|
)
|
|
|
|
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")
|
|
}
|
|
|
|
func getInitSystem() string {
|
|
if runtime.GOOS == "windows" {
|
|
return "windows"
|
|
}
|
|
if runtime.GOOS == "linux" {
|
|
if _, err := os.Stat("/run/systemd/system"); err == nil {
|
|
return "systemd"
|
|
}
|
|
if _, err := os.Stat("/etc/init.d"); err == nil {
|
|
return "init.d"
|
|
}
|
|
}
|
|
return "unknown"
|
|
} |