dnd-music-console/croaker/pidfile.py

22 lines
601 B
Python
Raw Normal View History

2024-03-01 01:00:17 -08:00
import logging
import os
import signal
from pathlib import Path
from daemon import pidfile as _pidfile
2024-03-05 22:21:56 -08:00
logger = logging.getLogger('daemon')
2024-03-01 01:00:17 -08:00
def pidfile(pidfile_path: Path, sig=signal.SIGQUIT, terminate_if_running: bool = True):
pf = _pidfile.TimeoutPIDLockFile(str(pidfile_path.expanduser()), 30)
2024-03-01 01:00:17 -08:00
pid = pf.read_pid()
if pid and terminate_if_running:
try:
2024-03-05 22:21:56 -08:00
logger.debug(f"Stopping PID {pid}")
os.kill(pid, sig)
2024-03-01 01:00:17 -08:00
except ProcessLookupError:
2024-03-05 22:21:56 -08:00
logger.debug(f"PID {pid} not running; breaking lock.")
2024-03-01 01:00:17 -08:00
pf.break_lock()
return pf