Created
December 1, 2017 11:37
-
-
Save s-m-e/fdf244394cf7ca85d036d7d5fd99b6e2 to your computer and use it in GitHub Desktop.
Script for programmatically checking weather a daemonocle daemon is up or down
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script for programmatically checking weather a daemonocle daemon is up or down | |
# -> see: https://github.com/jnrbsn/daemonocle | |
# -> works around the following issue: https://github.com/jnrbsn/daemonocle/issues/18 | |
# -> requires knowledge of the location of the daemon's pid file | |
# -> tested with CPython 3.6 | |
# Licenced under the MIT License (MIT) | |
# Copyright (c) 2017 @s-m-e | |
import os | |
import psutil # non-standard-library dependency: pip install psutil | |
class no_pid_error(Exception): | |
pass | |
def is_daemon_up(pid_file_path): | |
try: | |
_ = __get_daemon_pid__(pid_file_path) | |
return True | |
except no_pid_error: | |
return False | |
def __get_daemon_pid__(pid_file_path): | |
try: | |
f = open(pid_file_path, 'r') | |
pid_data = f.read() | |
f.close() | |
except FileNotFoundError: | |
raise no_pid_error() | |
try: | |
pid = int(pid_data) | |
except ValueError: | |
raise no_pid_error() | |
if pid is not None and psutil.pid_exists(pid): | |
return pid | |
else: | |
os.remove(pid_file_path) # daemonocle does that but you might as well want to skip it | |
raise no_pid_error() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment