fix: Agent 安装时自动清理已存在的旧服务

重新安装 Agent 时如果服务已存在会报错
"Init already exists: /etc/systemd/system/pika-agent.service"

修改 Install() 方法,判断错误类型:
- 如果是"服务已存在"错误,先停止卸载旧服务再重装
- 其他错误(如权限不足)直接返回

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
yumusbandClaude Opus 4.6 committed 2026-04-10 16:42:08 +08:00
1 parent 9c1752bb4d
commit e91d1a106b
1 file changed
+18 -1
+18 -1
View File
@@ -7,6 +7,7 @@ import (
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"github.com/dushixiang/pika/pkg/agent"
@@ -203,7 +204,23 @@ func NewServiceManager(cfg *config.Config) (*ServiceManager, error) {
// Install 安装服务
func (m *ServiceManager) Install() error {
return m.service.Install()
// 先尝试直接安装
err := m.service.Install()
if err == nil {
return nil
}
// 只有当是"服务已存在"的错误时才清理重装
if strings.Contains(err.Error(), "already exists") {
slog.Info("服务已存在,先清理旧服务再重新安装...")
_ = m.service.Stop()
_ = m.service.Uninstall()
// 再次尝试安装
return m.service.Install()
}
// 其他错误直接返回
return err
}
// Uninstall 卸载服务