欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)Linux下守護(hù)進(jìn)程的編寫方法

 更新時(shí)間:2014年08月22日 11:36:49   投稿:shichen2014  
這篇文章主要介紹了Python實(shí)現(xiàn)Linux下守護(hù)進(jìn)程的編寫方法,比較實(shí)用的一個(gè)技巧,需要的朋友可以參考下

本文實(shí)例講述了Python實(shí)現(xiàn)Linux下守護(hù)進(jìn)程的編寫方法,分享給大家供大家參考,相信對(duì)于大家的Python程序設(shè)計(jì)會(huì)起到一定的幫助作用。具體方法如下:

1. 調(diào)用fork()以便父進(jìn)程可以退出,這樣就將控制權(quán)歸還給運(yùn)行你程序的命令行或shell程序。需要這一步以便保證新進(jìn)程不是一個(gè)進(jìn)程組頭領(lǐng)進(jìn)程(process group leader)。下一步,‘setsid()',會(huì)因?yàn)槟闶沁M(jìn)程組頭領(lǐng)進(jìn)程而失敗。進(jìn)程調(diào)用fork函數(shù)時(shí),操作系統(tǒng)會(huì)新建一個(gè)子進(jìn)程,它本質(zhì)上與父進(jìn)程完全相同。子進(jìn)程從父進(jìn)程繼承了多個(gè)值的拷貝,比如全局變量和環(huán)境變量。兩個(gè)進(jìn)程唯一的區(qū)別就是fork的返回值。child(子)進(jìn)程接收返回值為0,而父進(jìn)程接收子進(jìn)程的pid作為返回值。調(diào)用fork函數(shù)后,兩個(gè)進(jìn)程并發(fā)執(zhí)行同一個(gè)程序,首先執(zhí)行的是調(diào)用了fork之后的下一行代碼。父進(jìn)程和子進(jìn)程既并發(fā)執(zhí)行,又相互獨(dú)立;也就是說(shuō),它們是“異步執(zhí)行”的。

2. 調(diào)用‘setsid()' 以便成為一個(gè)進(jìn)程組和會(huì)話組的頭領(lǐng)進(jìn)程。由于一個(gè)控制終端與一個(gè)會(huì)話相關(guān)聯(lián),而且這個(gè)新會(huì)話還沒有獲得一個(gè)控制終端,我們的進(jìn)程沒有控制終端,這對(duì)于守護(hù)程序來(lái)說(shuō)是一件好事。

3. 再次調(diào)用‘fork()'所以父進(jìn)程(會(huì)話組頭領(lǐng)進(jìn)程)可以退出。這意味著我們,一個(gè)非會(huì)話組頭領(lǐng)進(jìn)程永遠(yuǎn)不能重新獲得控制終端。

4. 調(diào)用‘chdir("/")'確認(rèn)我們的進(jìn)程不保持任何目錄于使用狀態(tài)。不做這個(gè)會(huì)導(dǎo)致系統(tǒng)管理員不能卸裝(umount)一個(gè)文件系統(tǒng),因?yàn)樗俏覀兊漠?dāng)前工作目錄。 [類似的,我們可以改變當(dāng)前目錄至對(duì)于守護(hù)程序運(yùn)行重要的文件所在目錄]

5. 調(diào)用‘umask(0)'以便我們擁有對(duì)于我們寫的任何東西的完全控制。我們不知道我們繼承了什么樣的umask。 [這一步是可選的](譯者注:這里指步驟5,因?yàn)槭刈o(hù)程序不一定需要寫文件)

6. 調(diào)用‘close()'關(guān)閉文件描述符0,1和2。這樣我們釋放了從父進(jìn)程繼承的標(biāo)準(zhǔn)輸入,標(biāo)準(zhǔn)輸出,和標(biāo)準(zhǔn)錯(cuò)誤輸出。我們沒辦法知道這些文描述符符可能已經(jīng)被重定向去哪里。注意到許多守護(hù)程序使用‘sysconf()'來(lái)確認(rèn)‘_SC_OPEN_MAX'的限制?!甠SC_OPEN_MAX'告訴你每個(gè)進(jìn)程能夠打開的最多文件數(shù)。然后使用一個(gè)循環(huán),守護(hù)程序可以關(guān)閉所有可能的文件描述符。你必須決定你需要做這個(gè)或不做。如果你認(rèn)為有可能有打開的文件描述符,你需要關(guān)閉它們,因?yàn)橄到y(tǒng)有一個(gè)同時(shí)打開文件數(shù)的限制。

7. 為標(biāo)準(zhǔn)輸入,標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤輸出建立新的文件描述符。即使你不打算使用它們,打開著它們不失為一個(gè)好主意。準(zhǔn)確操作這些描述符是基于各自愛好;比如說(shuō),如果你有一個(gè)日志文件,你可能希望把它作為標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤輸出打開,而把‘/dev/null'作為標(biāo)準(zhǔn)輸入打開;作為替代方法,你可以將‘/dev/console'作為標(biāo)準(zhǔn)錯(cuò)誤輸出和/或標(biāo)準(zhǔn)輸出打開,而‘/dev/null'作為標(biāo)準(zhǔn)輸入,或者任何其它對(duì)你的守護(hù)程序有意義的結(jié)合方法。(譯者注:一般使用dup2函數(shù)原子化關(guān)閉和復(fù)制文件描述符。

實(shí)現(xiàn)代碼如下:

# Core modules 
importatexit 
importos 
importsys 
importtime 
importsignal 
classDaemon(object): 
  """ 
A generic daemon class. 
Usage: subclass the Daemon class and override the run() method 
""" 
  def __init__(self, pidfile, stdin=os.devnull, 
         stdout=os.devnull, stderr=os.devnull, 
         home_dir='.', umask=022, verbose=1): 
    self.stdin = stdin 
    self.stdout = stdout 
    self.stderr = stderr 
    self.pidfile = pidfile 
    self.home_dir = home_dir 
    self.verbose = verbose 
    self.umask = umask 
    self.daemon_alive = True 
  def daemonize(self): 
    """ 
Do the UNIX double-fork magic, see Stevens' "Advanced 
Programming in the UNIX Environment" for details (ISBN 0201563177) 
""" 
    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(self.home_dir) 
    os.setsid() 
    os.umask(self.umask) 
    # 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) 
    if sys.platform != 'darwin': # This block breaks on OS X 
      # Redirect standard file descriptors 
      sys.stdout.flush() 
      sys.stderr.flush() 
      si = file(self.stdin, 'r') 
      so = file(self.stdout, 'a+') 
      if self.stderr: 
        se = file(self.stderr, 'a+', 0) 
      else: 
        se = so 
      os.dup2(si.fileno(), sys.stdin.fileno()) 
      os.dup2(so.fileno(), sys.stdout.fileno()) 
      os.dup2(se.fileno(), sys.stderr.fileno()) 
    def sigtermhandler(signum, frame): 
      self.daemon_alive = False 
      signal.signal(signal.SIGTERM, sigtermhandler) 
      signal.signal(signal.SIGINT, sigtermhandler) 
    if self.verbose >= 1: 
      print "Started" 
    # Write pidfile 
    atexit.register( 
      self.delpid) # Make sure pid file is removed if we quit 
    pid = str(os.getpid()) 
    file(self.pidfile, 'w+').write("%s\n" % pid) 
  def delpid(self): 
    os.remove(self.pidfile) 
  def start(self, *args, **kwargs): 
    """ 
Start the daemon 
""" 
    if self.verbose >= 1: 
      print "Starting..." 
    # 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 
    except SystemExit: 
      pid = None 
    if pid: 
      message = "pidfile %s already exists. Is it already running?\n" 
      sys.stderr.write(message % self.pidfile) 
      sys.exit(1) 
    # Start the daemon 
    self.daemonize() 
    self.run(*args, **kwargs) 
  def stop(self): 
    """ 
Stop the daemon 
""" 
    if self.verbose >= 1: 
      print "Stopping..." 
    # Get the pid from the pidfile 
    pid = self.get_pid() 
    if not pid: 
      message = "pidfile %s does not exist. Not running?\n" 
      sys.stderr.write(message % self.pidfile) 
      # Just to be sure. A ValueError might occur if the PID file is 
      # empty but does actually exist 
      if os.path.exists(self.pidfile): 
        os.remove(self.pidfile) 
      return # Not an error in a restart 
    # Try killing the daemon process 
    try: 
      i = 0 
      while 1: 
        os.kill(pid, signal.SIGTERM) 
        time.sleep(0.1) 
        i = i + 1 
        if i % 10 == 0: 
          os.kill(pid, signal.SIGHUP) 
    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) 
    if self.verbose >= 1: 
      print "Stopped" 
  def restart(self): 
    """ 
Restart the daemon 
""" 
    self.stop() 
    self.start() 
  def get_pid(self): 
    try: 
      pf = file(self.pidfile, 'r') 
      pid = int(pf.read().strip()) 
      pf.close() 
    except IOError: 
      pid = None 
    except SystemExit: 
      pid = None 
    return pid 
  def is_running(self): 
    pid = self.get_pid() 
    print(pid) 
    return pid and os.path.exists('/proc/%d' % pid) 
  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(). 
"""

感興趣的讀者可以調(diào)試運(yùn)行一下本文實(shí)例代碼,相信會(huì)有新的收獲。

相關(guān)文章

最新評(píng)論