Created
May 4, 2016 13:42
-
-
Save junnplus/8d8f55a069584b2ce795b38a64e1488b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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