From 3e3adf122c61ad4a4a31c6c9385cd12a44946310 Mon Sep 17 00:00:00 2001 From: NanamiAdmin Date: Tue, 21 Apr 2026 18:00:39 +0800 Subject: [PATCH] fix(instance): (systemd) fix when the instance is not running but the backend still says it's running --- frpAct.go | 11 ++++++++--- os.go | 30 +++++++++++++++++------------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/frpAct.go b/frpAct.go index 90cdaa9..1a5ae73 100644 --- a/frpAct.go +++ b/frpAct.go @@ -635,13 +635,18 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) { if is.watchdogConnected { if !watchdog.AddInstance(serviceName) { + if err := IsInstanceRunning(instanceID); err != nil { // Check if instance is really running + SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to start instance %d: %v", instanceID, err)) + postLog.Warning(fmt.Sprintf("[StartInstanceHandler] Instance %d may not be running: %v", instanceID, err)) + return + } postLog.Warning(fmt.Sprintf("[StartInstanceHandler] Failed to add watchdog instance %s", serviceName)) SendSuccessResponse(w, "Instance started successfully but watchdog instance add failed", nil) return } } - if err := IsInstanceRunning(instanceID); err != nil { + if err := IsInstanceRunning(instanceID); err != nil { // Check if instance is really running SendErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Failed to start instance %d: %v", instanceID, err)) postLog.Warning(fmt.Sprintf("[StartInstanceHandler] Instance %d may not be running: %v", instanceID, err)) return @@ -915,7 +920,7 @@ func GetInstanceStatusHandler(w http.ResponseWriter, r *http.Request) { } SendSuccessResponse(w, "Instance status retrieved successfully", responseData) - postLog.Info(fmt.Sprintf("[GetInstanceStatusHandler] Retrieved status for instance %d (name: %s), isRunning: %v", instanceID, instance.Name, responseData["isRunning"])) + // postLog.Debug(fmt.Sprintf("[GetInstanceStatusHandler] Retrieved status for instance %d (name: %s), isRunning: %v", instanceID, instance.Name, responseData["isRunning"])) } func GetInstanceInfoHandler(w http.ResponseWriter, r *http.Request) { @@ -1010,5 +1015,5 @@ func GetInstanceInfoHandler(w http.ResponseWriter, r *http.Request) { } SendSuccessResponse(w, "Instance info retrieved successfully", responseData) - postLog.Info(fmt.Sprintf("[GetInstanceInfoHandler] Retrieved info for instance %d (name: %s), userType: %s", instanceID, instance.Name, userType)) + // postLog.Debug(fmt.Sprintf("[GetInstanceInfoHandler] Retrieved info for instance %d (name: %s), userType: %s", instanceID, instance.Name, userType)) } diff --git a/os.go b/os.go index 7b520f2..6b4ac2f 100644 --- a/os.go +++ b/os.go @@ -412,7 +412,7 @@ func IsInstanceRunning(instanceID int) error { output, err := cmd.CombinedOutput() postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Sc query output: %s", output)) outputStr := string(output) - + if err != nil { postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to check Windows service status: %s, output: %s", err, output)) return err @@ -427,33 +427,37 @@ func IsInstanceRunning(instanceID int) error { } case "systemd": - cmd := exec.Command("systemctl", "is-active", serviceName) + cmd := exec.Command("systemctl", "status", serviceName) output, err := cmd.CombinedOutput() - status := strings.TrimSpace(string(output)) + outputStr := string(output) if err != nil { exitError, ok := err.(*exec.ExitError) if ok { exitCode := exitError.ExitCode() - if exitCode == 1 && status == "inactive" { - postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is stopped", serviceName)) - return fmt.Errorf("service %s is stopped", serviceName) + if exitCode == 1 { + postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service status command failed with exit code 1: %s, output: %s", serviceName, outputStr)) + return fmt.Errorf("service %s is not running", serviceName) } - if exitCode == 3 && status == "inactive" { - postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is inactive", serviceName)) - return fmt.Errorf("service %s is inactive", serviceName) + if exitCode == 2 { + postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service status command failed with exit code 2: %s, output: %s", serviceName, outputStr)) + return fmt.Errorf("service %s is not running", serviceName) } - if exitCode == 4 && status == "unknown" { - postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s does not exist", serviceName)) + if exitCode == 3 { + postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is not running: %s, output: %s", serviceName, outputStr)) + return fmt.Errorf("service %s is not running", serviceName) + } + if exitCode == 4 { + postLog.Debug(fmt.Sprintf("[IsInstanceRunning] Systemd service %s does not exist: %s, output: %s", serviceName, outputStr)) return fmt.Errorf("service %s does not exist", serviceName) } } - postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to check systemd service status: %s, output: %s", err, output)) + postLog.Error(fmt.Sprintf("[IsInstanceRunning] Failed to check systemd service status: %s, output: %s", err, outputStr)) return err } - if status == "active" { + if strings.Contains(outputStr, "active (running)") { postLog.Info(fmt.Sprintf("[IsInstanceRunning] Systemd service %s is running", serviceName)) return nil } else {