fix(instance): (systemd) fix when the instance is not running but the backend still says it's running
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user