本文實例講述了Python實現Linux下守護進程的編寫方法,分享給大家供大家參考,相信對于大家的Python程序設計會起到一定的幫助作用。具體方法如下:
1. 調用fork()以便父進程可以退出,這樣就將控制權歸還給運行你程序的命令行或shell程序。 需要這一步以便保證新進程不是一個進程組頭領進程(process group leader)。下一步,‘setsid()',會因為你是進程組頭領進程而失敗。進程調用fork函數時,操作系統會新建一個子進程,它本質上與父進程完全相同。子進程從父進程繼承了多個值的拷貝,比如全局變量和環境變量。兩個進程唯一的區別就是fork的返回值。child(子)進程接收返回值為0,而父進程接收子進程的pid作為返回值。調用fork函數后,兩個進程并發執行同一個程序,首先執行的是調用了fork之后的下一行代碼。父進程和子進程既并發執行,又相互獨立;也就是說,它們是“異步執行”的。
2. 調用‘setsid()' 以便成為一個進程組和會話組的頭領進程 。由于一個控制終端與一個會話相關聯,而且這個新會話還沒有獲得一個控制終端,我們的進程沒有控制終端,這對于守護程序來說是一件好事。
3. 再次調用‘fork()'所以父進程(會話組頭領進程)可以退出 。這意味著我們,一個非會話組頭領進程永遠不能重新獲得控制終端。
4. 調用‘chdir("/")'確認我們的進程不保持任何目錄于使用狀態 。不做這個會導致系統管理員不能卸裝(umount)一個文件系統,因為它是我們的當前工作目錄。 [類似的,我們可以改變當前目錄至對于守護程序運行重要的文件所在目錄]
5. 調用‘umask(0)'以便我們擁有對于我們寫的任何東西的完全控制。 我們不知道我們繼承了什么樣的umask。 [這一步是可選的](譯者注:這里指步驟5,因為守護程序不一定需要寫文件)
6. 調用‘close()'關閉文件描述符0,1和2。這樣我們釋放了從父進程繼承的標準輸入,標準輸出,和標準錯誤輸出。 我們沒辦法知道這些文描述符符可能已經被重定向去哪里。注意到許多守護程序使用‘sysconf()'來確認‘_SC_OPEN_MAX'的限制?!甠SC_OPEN_MAX'告訴你每個進程能夠打開的最多文件數。然后使用一個循環,守護程序可以關閉所有可能的文件描述符。你必須決定你需要做這個或不做。如果你認為有可能有打開的文件描述符,你需要關閉它們,因為系統有一個同時打開文件數的限制。
7. 為標準輸入,標準輸出和標準錯誤輸出建立新的文件描述符。 即使你不打算使用它們,打開著它們不失為一個好主意。準確操作這些描述符是基于各自愛好;比如說,如果你有一個日志文件,你可能希望把它作為標準輸出和標準錯誤輸出打開,而把‘/dev/null'作為標準輸入打開;作為替代方法,你可以將‘/dev/console'作為標準錯誤輸出和/或標準輸出打開,而‘/dev/null'作為標準輸入,或者任何其它對你的守護程序有意義的結合方法。(譯者注:一般使用dup2函數原子化關閉和復制文件描述符。
實現代碼如下:
# 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().
"""
感興趣的讀者可以調試運行一下本文實例代碼,相信會有新的收獲。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

