Skip to content

Commit

Permalink
fix issues in enabling/disabling service (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerinus authored Mar 29, 2024
1 parent aa24051 commit 083e7ec
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions utils/systemctl/systemctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 083e7ec

Please sign in to comment.