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:
@@ -0,0 +1,41 @@
|
||||
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"
|
||||
}
|
||||
Reference in New Issue
Block a user