Check if daemon is already running. Returns True if another daemon is running. Removes stale PID file if process is dead.
()
| 44 | # ==================== PID File Management ==================== |
| 45 | |
| 46 | def check_pid_file() -> bool: |
| 47 | """ |
| 48 | Check if daemon is already running. |
| 49 | |
| 50 | Returns True if another daemon is running. |
| 51 | Removes stale PID file if process is dead. |
| 52 | """ |
| 53 | if PID_FILE.exists(): |
| 54 | try: |
| 55 | pid = int(PID_FILE.read_text().strip()) |
| 56 | os.kill(pid, 0) # Signal 0 checks if process exists |
| 57 | return True # Daemon is running |
| 58 | except PermissionError: |
| 59 | return True # Process exists but owned by another user — treat as running |
| 60 | except (ProcessLookupError, ValueError): |
| 61 | # Process is dead or PID file is corrupt — remove stale file |
| 62 | warning("Removing stale PID file") |
| 63 | PID_FILE.unlink(missing_ok=True) |
| 64 | return False |
| 65 | return False |
| 66 | |
| 67 | |
| 68 | def write_pid_file(): |
no test coverage detected