Skip to content

Instantly share code, notes, and snippets.

@y-ogi
Created October 22, 2013 10:27
Show Gist options
  • Save y-ogi/7098329 to your computer and use it in GitHub Desktop.
Save y-ogi/7098329 to your computer and use it in GitHub Desktop.
python daemon sample
# -*- 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