Skip to content

Instantly share code, notes, and snippets.

@junnplus
Created May 4, 2016 13:42
Show Gist options
  • Save junnplus/8d8f55a069584b2ce795b38a64e1488b to your computer and use it in GitHub Desktop.
Save junnplus/8d8f55a069584b2ce795b38a64e1488b to your computer and use it in GitHub Desktop.
import os
import sys
import time
class Deamon(object):
def __init__(self,
stdin='/dev/null',
stdout='/dev/null',
stderr='/dev/null'):
self.stdin = stdin
self.stdout = stdout
self.stderr = stderr
def deamonize(self):
if os.fork():
os._exit(0)
os.setsid()
if os.fork():
os._exit(0)
os.chdir('/')
si = open(self.stdin, 'r')
so = open(self.stdout, 'a+')
se = open(self.stderr, 'a+', 0)
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
def start(self):
self.deamonize()
self.run()
def run(self):
raise NotImplementedError
class MyDeamon(Deamon):
def run(self):
while True:
time.sleep(1)
if __name__ == '__main__':
d = MyDeamon()
d.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment