Skip to content

Instantly share code, notes, and snippets.

@carlopires
Created October 24, 2011 17:57
Show Gist options
  • Save carlopires/1309650 to your computer and use it in GitHub Desktop.
Save carlopires/1309650 to your computer and use it in GitHub Desktop.
Easy python daemon
# -*- coding: utf-8 -*-
"""
Created on 23/10/2011
@author: Carlo Pires <carlopires@gmail.com>
"""
import sys
import time
from daemon import DaemonContext
from daemon.runner import make_pidlockfile, is_pidfile_stale
pidfile = make_pidlockfile('/tmp/daemon.pid', 0)
if is_pidfile_stale(pidfile):
pidfile.break_lock()
if pidfile.is_locked():
print 'daemon is running with PID %s already' % pidfile.read_pid()
sys.exit(0)
with DaemonContext(pidfile=pidfile):
for i in range(1,30):
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment