2023-07-31 03:28:41 +00:00
|
|
|
package systemctl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-10-17 09:52:34 +00:00
|
|
|
"github.com/pkg/errors"
|
2023-10-23 02:37:35 +00:00
|
|
|
"os/exec"
|
2023-07-31 03:28:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func RunSystemCtl(args ...string) (string, error) {
|
|
|
|
cmd := exec.Command("systemctl", args...)
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return string(output), fmt.Errorf("failed to run command: %w", err)
|
|
|
|
}
|
|
|
|
return string(output), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsActive(serviceName string) (bool, error) {
|
|
|
|
out, err := RunSystemCtl("is-active", serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return out == "active\n", nil
|
|
|
|
}
|
|
|
|
|
2023-10-17 09:52:34 +00:00
|
|
|
func IsEnable(serviceName string) (bool, error) {
|
|
|
|
out, err := RunSystemCtl("is-enabled", serviceName)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return out == "enabled\n", nil
|
|
|
|
}
|
|
|
|
|
2023-10-23 02:37:35 +00:00
|
|
|
// IsExist checks if a service exists.
|
2023-07-31 03:28:41 +00:00
|
|
|
func IsExist(serviceName string) (bool, error) {
|
2023-10-23 02:37:35 +00:00
|
|
|
cmd := exec.Command("systemctl", "is-enabled", serviceName)
|
|
|
|
output, err := cmd.CombinedOutput()
|
2023-07-31 03:28:41 +00:00
|
|
|
if err != nil {
|
2023-10-23 02:37:35 +00:00
|
|
|
// If the command fails, check if the output indicates that the service does not exist.
|
|
|
|
if string(output) == fmt.Sprintf("Failed to get unit file state for %s.service: No such file or directory\n", serviceName) {
|
|
|
|
// Return false if the service does not exist.
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
// Return an error if the command fails.
|
|
|
|
return false, fmt.Errorf("failed to run command: %w", err)
|
2023-07-31 03:28:41 +00:00
|
|
|
}
|
2023-10-23 02:37:35 +00:00
|
|
|
// Return true if the service exists.
|
|
|
|
return true, nil
|
2023-07-31 03:28:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func handlerErr(out string, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
if out != "" {
|
|
|
|
return errors.New(out)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Restart(serviceName string) error {
|
|
|
|
out, err := RunSystemCtl("restart", serviceName)
|
|
|
|
return handlerErr(out, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Operate(operate, serviceName string) error {
|
|
|
|
out, err := RunSystemCtl(operate, serviceName)
|
|
|
|
return handlerErr(out, err)
|
|
|
|
}
|