1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-03-13 17:24:44 +08:00

feat: 优化判断systemctl的service文件是否存在函数 (#2615)

This commit is contained in:
igophper 2023-10-23 10:37:35 +08:00 committed by GitHub
parent a86ec12b86
commit 523f1ea67f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,10 +2,8 @@ package systemctl
import (
"fmt"
"os/exec"
"strings"
"github.com/pkg/errors"
"os/exec"
)
func RunSystemCtl(args ...string) (string, error) {
@ -33,12 +31,21 @@ func IsEnable(serviceName string) (bool, error) {
return out == "enabled\n", nil
}
// IsExist checks if a service exists.
func IsExist(serviceName string) (bool, error) {
out, err := RunSystemCtl("list-unit-files")
cmd := exec.Command("systemctl", "is-enabled", serviceName)
output, err := cmd.CombinedOutput()
if err != nil {
return false, err
// 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)
}
return strings.Contains(out, serviceName+".service"), nil
// Return true if the service exists.
return true, nil
}
func handlerErr(out string, err error) error {