Created
October 22, 2013 10:27
-
-
Save y-ogi/7098329 to your computer and use it in GitHub Desktop.
python daemon sample
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
# -*- coding: utf-8 -*- | |
import sys | |
import time | |
import argparse | |
from daemon import DaemonContext | |
from daemon.pidfile import PIDLockFile | |
def daemon_loop(interval=600): | |
while True: | |
print 'hogehoge' | |
time.sleep(interval) | |
def parse_args(): | |
parser = argparse.ArgumentParser(description='Sample Daemon') | |
parser.add_argument('-i', '--interval', help='interval', type=int, default=600) | |
parser.add_argument('-p', '--pidfile', help='pid file path', type=str, default='/tmp/sample.pid') | |
return parser.parse_args() | |
if __name__ == '__main__': | |
# parse argments | |
args = parse_args() | |
context = DaemonContext( | |
stdout=sys.stdout, | |
stderr=sys.stderr, | |
pidfile=PIDLockFile(args.pidfile), | |
) | |
with context: | |
daemon_loop(interval=args.interval) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment