Created
July 30, 2015 14:37
-
-
Save mgmarino/2df6ecff0de3536c5c16 to your computer and use it in GitHub Desktop.
Example for using python-daemon and runner.DaemonRunner
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
import time | |
import os | |
import logging | |
from daemon.runner import DaemonRunner | |
_base_path = "/path/to/daemon" | |
class MyApp(object): | |
""" | |
Define the required attributes | |
""" | |
stdin_path = "/dev/null" | |
stdout_path = os.path.join(_base_path, "myapp.out") # Can also be /dev/null | |
stderr_path = os.path.join(_base_path, "myapp.err") # Can also be /dev/null | |
pidfile_path = os.path.join(_base_path, "myapp.pid") | |
pidfile_timeout = 3 | |
def run(self): | |
#Basic logging | |
logging.basicConfig(format="%(asctime)s [ARDUINO/%(processName)s] %(levelname)s %(message)s", | |
filename=os.path.join(_base_path, "app.out"), | |
level=logging.INFO) | |
logging.info("Starting") | |
try: | |
with open(os.path.join(_base_path,'spam.data'), 'w') as o: | |
while True: | |
# Loop will be broken by signal | |
o.write("Hey\n") | |
logging.info("Hearbeat") | |
time.sleep(1) | |
o.write("Done\n") | |
except (SystemExit,KeyboardInterrupt): | |
# Normal exit getting a signal from the parent process | |
pass | |
except: | |
# Something unexpected happened? | |
logging.exception("Exception") | |
finally: | |
logging.info("Finishing") | |
if __name__ == '__main__': | |
""" | |
Call with arguments start/restart/stop | |
""" | |
run = DaemonRunner(MyApp()) | |
run.do_action() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment