From 083e7ecc04d5a5313461cca4dfe995c905e41936 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 29 Mar 2024 16:41:52 +0800 Subject: [PATCH] fix issues in enabling/disabling service (#48) --- utils/systemctl/systemctl.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/utils/systemctl/systemctl.go b/utils/systemctl/systemctl.go index c882e60..a8014ed 100644 --- a/utils/systemctl/systemctl.go +++ b/utils/systemctl/systemctl.go @@ -139,7 +139,7 @@ func IsServiceRunning(name string) (bool, error) { return property.Value.Value() == "active", nil } -func EnableService(name string) error { +func EnableService(nameOrPath string) error { // connect to systemd ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() @@ -151,11 +151,13 @@ func EnableService(name string) error { defer conn.Close() - _, _, err = conn.EnableUnitFilesContext(ctx, []string{name}, false, true) + _, _, err = conn.EnableUnitFilesContext(ctx, []string{nameOrPath}, false, true) if err != nil { return err } + name := filepath.Base(nameOrPath) + // ensure service is enabled property, err := conn.GetUnitPropertyContext(ctx, name, "ActiveState") if err != nil { @@ -188,7 +190,7 @@ func DisableService(name string) error { } if properties["ActiveState"] == "active" { - return StopService(name) + _ = StopService(name) // don't care about the result } _, err = conn.DisableUnitFilesContext(ctx, []string{name}, false)