Created
May 13, 2016 14:48
-
-
Save trapatsas/2913ca311805d6eef5d1955fe81ba733 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import logging | |
import time | |
import daemon | |
from daemon import pidfile as pidlockfile | |
from daemon import runner as rnr | |
import signal | |
import setproctitle | |
# Setting logging configuration | |
logger = logging.getLogger("MyScript") | |
logger.setLevel(logging.DEBUG) | |
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") | |
handler = logging.FileHandler("/tmp/myscript.log") | |
handler.setFormatter(formatter) | |
logger.addHandler(handler) | |
print(daemon.__file__) | |
# Rename this process so 'ps' output looks like this is a native | |
# executable. Can not seperate command-line arguments from actual name of | |
# the executable by NUL bytes, so only show the name of the executable | |
# instead. setproctitle.setproctitle("\x00".join(sys.argv)) | |
setproctitle.setproctitle('mitsos.service') | |
pid = pidlockfile.TimeoutPIDLockFile("/tmp/myscript.pid", 10) | |
# Remove any stale PID files, left behind by previous invocations | |
if rnr.is_pidfile_stale(pid): | |
logging.warning("Removing stale PID lock file %s", pid.path) | |
pid.break_lock() | |
context = daemon.DaemonContext( | |
#working_directory='/var/lib/foo', | |
umask=0o002, | |
pidfile=pid, | |
files_preserve=[handler.stream], | |
) | |
loop = True | |
def program_cleanup_test(): | |
logger.info("Stopping loop") | |
loop = False | |
context.signal_map = { | |
signal.SIGTERM: program_cleanup_test, | |
signal.SIGHUP: 'terminate', | |
#signal.SIGUSR1: reload_program_config, | |
} | |
print("Running as a daemon") | |
with context: | |
while loop: | |
logger.info("0255") | |
time.sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment