Last active
April 4, 2020 04:50
-
-
Save timss/883b3b4a51b0db7d0e0d to your computer and use it in GitHub Desktop.
A combination of python-daemon and APScheduler
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
#!/usr/bin/env python | |
import apscheduler.scheduler | |
import daemon.runner | |
import os.path | |
import sys | |
import time | |
class Core(): | |
def __init__(self): | |
"""Daemon behaviour""" | |
self.stdin_path = '/dev/null' | |
self.stdout_path = '/dev/tty' | |
self.stderr_path = '/dev/tty' | |
self.pidfile_path = os.path.realpath('core.pid') | |
self.pidfile_timeout = 5 | |
self.sched = apscheduler.scheduler.Scheduler() | |
self.sched.standalone = True | |
def run(self): | |
"""Main daemon loop""" | |
try: | |
@self.sched.interval_schedule(seconds=5) | |
def running(): | |
print "Running..." | |
self.sched.start() | |
except: | |
self.sched.shutdown() | |
print "Unexpected error:", sys.exc_info() | |
sys.exit(1) | |
if __name__ == "__main__": | |
if len(sys.argv) == 2: | |
core = Core() | |
runner = daemon.runner.DaemonRunner(core) | |
runner.do_action() # start|stop|restart as sys.argv[1] | |
else: | |
print "Usage: %s start|stop|restart" % sys.argv[0] | |
sys.exit(1) | |
else: | |
print "Core daemon can't be included in another program." | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment