cleanupPIDFile removes the PID file if it was written by this process.
(pidFile string, logger *slog.Logger)
| 338 | |
| 339 | // cleanupPIDFile removes the PID file if it was written by this process. |
| 340 | func cleanupPIDFile(pidFile string, logger *slog.Logger) { |
| 341 | data, err := os.ReadFile(pidFile) |
| 342 | if err != nil { |
| 343 | if !os.IsNotExist(err) { |
| 344 | logger.Error("Failed to read PID file for cleanup", "pidFile", pidFile, "error", err) |
| 345 | } |
| 346 | return |
| 347 | } |
| 348 | pidStr := strings.TrimSpace(string(data)) |
| 349 | filePID, err := strconv.Atoi(pidStr) |
| 350 | if err != nil || filePID != os.Getpid() { |
| 351 | logger.Info("PID file belongs to another process, skipping cleanup", "pidFile", pidFile, "filePID", pidStr) |
| 352 | return |
| 353 | } |
| 354 | if err := os.Remove(pidFile); err != nil && !os.IsNotExist(err) { |
| 355 | logger.Error("Failed to remove PID file", "pidFile", pidFile, "error", err) |
| 356 | } else if err == nil { |
| 357 | logger.Info("Removed PID file", "pidFile", pidFile) |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | type flagSpec struct { |
| 362 | name string |
no outgoing calls