From 2aca93f36be19f059b2fe670f68240c62a08ebff Mon Sep 17 00:00:00 2001 From: tian <11429339@qq.com> Date: Tue, 21 Jul 2026 09:47:51 +0800 Subject: [PATCH] fix: parse systemctl ExecStart path= format correctly --- agent/internal/procctl/systemctl.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/agent/internal/procctl/systemctl.go b/agent/internal/procctl/systemctl.go index a1c1da1..0cf3eae 100644 --- a/agent/internal/procctl/systemctl.go +++ b/agent/internal/procctl/systemctl.go @@ -56,20 +56,36 @@ func (s *SystemCtlController) Status() (Status, error) { func (s *SystemCtlController) Version() (string, error) { // 从 systemctl 获取 ExecStart 路径,再调用 --version + // Output format: { path=/path/to/binary ; argv[]=... } binOut, err := exec.Command("systemctl", "show", "--property=ExecStart", "--value", s.serviceName).Output() if err != nil { return "", fmt.Errorf("get version failed: %w", err) } - binPath := strings.Fields(strings.TrimSpace(string(binOut)))[0] + out := strings.TrimSpace(string(binOut)) + // Parse path= from systemctl ExecStart format + const prefix = "path=" + idx := strings.Index(out, prefix) + if idx < 0 { + return "", fmt.Errorf("get version failed: cannot parse ExecStart") + } + rest := out[idx+len(prefix):] + end := strings.IndexByte(rest, ' ') + if end < 0 { + end = strings.IndexByte(rest, ';') + } + if end < 0 { + end = len(rest) + } + binPath := rest[:end] if binPath == "" { return "", fmt.Errorf("get version failed: empty ExecStart") } cmd := exec.Command(binPath, "--version") - out, err := cmd.Output() + out2, err := cmd.Output() if err != nil { return "", fmt.Errorf("get version failed: %w", err) } - return strings.TrimSpace(string(out)), nil + return strings.TrimSpace(string(out2)), nil } func (s *SystemCtlController) Start(configName string) (Status, error) {