Looking for the whole day, finally found a code that can be used
#! /usr/bin/env python2.7#encoding:utf-8# @description: Example of a python daemon # @tags:p ython,daemonimport sysimport osimport timeimport atexitfrom signal import SIGTERM class Daemon: "" " A generic daemon class. usage: subclass the daemon class and override the run () method "" " def __init__ (self, pidfile, stdin= '/dev/null ', stdout= '/ Dev/null ', stderr= '/dev/null '): self.stdin = stdin self.stdout = stdout self.stderr = stderr self.pidfile = pidfile def daemonize (self): "" do the unix double-fork magic, see stevens " "Advanced &nbsP; programming in the unix environment " for details (isbn 0201563177) http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16 "" " try: pid = os.fork () if pid > 0: # exit first parent sys.exit (0) except oserror, e: sys.stderr.write ("fork #1 failed: %d (%s) \ n" % (e.errno, e.strerror)) sys.exit (1) # decouple from parent Environment os.chdir ("/") os.setsid () os.umask (0)   &Nbsp; # do second fork try: pid = os.fork () if pid > 0: # exit from second parent sys.exit (0) except oserror, e: sys.stderr.write ("fork #2 failed: %d (%s ) \ n " % (E.errno, e.strerror)) sys.exit (1) # redirect standard file descriptors Sys.stdout.flush () sys.stderr.flush () si = file ( self.stdin, ' R ') so = file (self.stdout, ' A + ') se = file (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 ()) # write pidfile Atexit.register (self.delpid) pid = str (Os.getpid ()) File (self.pidfile, ' w+ '). Write ("%s\n" % pid) def delpid (self): os.remove (Self.pidfile) def start (self): "" " Start the daemon "" " # check for a pidfile to see if the daemon already runs try: pf = file (Self.pidfile, ' R ') pid = int (PF.read (). Strip ()) pf.close () except ioerror: pid = None if pid: message = "pidfile %s already exist. daemon Already running?\n " sys.stderr.write (message % self.pidfile) sys.exit (1) # start the daemon self.daemonize () self.run () def stop (self): "" " Stop the daemon "" " # get the pid from the pidfile try: pf = file (Self.pidfile, ' R ') Pid = int (Pf.read (). Strip ()) pf.close () except ioerror: pid = none if not pid: message = "Pidfile %s does not exist. daemon not running?\n " Sys.stderr.write (Message % self.pidfile) return # not an error in a restart # try killing the daemon process try: while 1: os.kill (pid, sigterm) time.sleep (0.1) except OSError, err:  ERR = STR (ERR) if err.find ("no such process") > 0: if os.path.exists (Self.pidfile): os.remove (Self.pidfile) else:     PRINT STR (Err) sys.exit (1) def restart (self): "" " Restart the daemon "" " self.stop () self.start () def run (self): "" " You should override this method when you subclass daemon. it will be called after the process has been daemonized by Start () or restart () . "" " class mydaemon (Daemon): def run (self): while true: time.sleep ( ) print ' daemon runing ' if __name__ == ' __main__ ': Daemon = mydaemon ("/var/run/demodaemon.pid") if len (SYS.ARGV) >= 2: if ' Start ' == sys.argv[1]: Daemon.start () elif ' Stop ' == sys.argv[1]: daemon.stop () elif ' Restart ' == sys.argv[1]: daemon.restart () else: print "Unknown command" sys.exit (2) sys.exit (0) else: print "Usage: %s start|stop|restart" % sys.argv[0] Sys.exit (2)
Bookmark a daemon code that implements a system-like service
Usage:
chmod +x xxx.py
xxx.py Start/stop/restart
Reference from:
Http://www.tuicool.com/articles/2ANbym
http://blog.csdn.net/dysj4099/article/details/18219411
#python # Daemon Implementation