package system import ( "context" "errors" "fmt" "os" "runtime" "strconv" "time" ) // Reboot schedules a host reboot a few seconds after the API response has // been flushed. systemd-run creates an independent transient timer, so the // reboot survives the panel process being stopped during shutdown. func Reboot(ctx context.Context) (string, error) { if runtime.GOOS != "linux" { return "", ErrUnsupported } if os.Geteuid() != 0 { return "", errors.New("只有 root 可以重启服务器") } runner := NewRunner() runner.Timeout = 10 * time.Second if Lookup("systemd-run") { unit := "mp-reboot-" + strconv.FormatInt(time.Now().Unix(), 10) if _, err := runner.Run(ctx, "systemd-run", "--collect", "--on-active=2s", "--unit="+unit, "systemctl", "reboot"); err == nil { return "systemd-run", nil } } if Lookup("systemctl") { if _, err := runner.Run(ctx, "systemctl", "reboot", "--no-block"); err == nil { return "systemctl", nil } } if Lookup("shutdown") { if _, err := runner.Run(ctx, "shutdown", "-r", "+1"); err == nil { return "shutdown", nil } } return "", fmt.Errorf("没有可用的重启命令(systemd-run/systemctl/shutdown)") }