fix: parse systemctl ExecStart path= format correctly

This commit is contained in:
tian 2026-07-21 09:47:51 +08:00
parent 32a5913c4d
commit 2aca93f36b

View File

@ -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) {