feat: improve daemon check by logging existing PIDs to prevent duplicate instances

This commit is contained in:
Menno van Leeuwen 2025-05-21 00:08:53 +02:00
parent 72705afc16
commit b2d9167b95
Signed by: vleeuwenmenno
SSH Key Fingerprint: SHA256:OJFmjANpakwD3F2Rsws4GLtbdz1TJ5tkQF0RZmF0TRE

View File

@ -28,13 +28,17 @@ func NewWatchCmd(history *models.History) *cobra.Command {
if err == nil {
pids := strings.Fields(string(out))
myPid := os.Getpid()
otherPids := []string{}
for _, pidStr := range pids {
pid, err := strconv.Atoi(pidStr)
if err == nil && pid != myPid {
log.Warn().Msg("kcm daemon is already running. Exiting to avoid duplicate daemons.")
os.Exit(1)
otherPids = append(otherPids, pidStr)
}
}
if len(otherPids) > 0 {
log.Warn().Strs("pids", otherPids).Msg("kcm daemon is already running. Exiting to avoid duplicate daemons.")
os.Exit(1)
}
}
// Set log file path since we are daemonizing