Python實(shí)現(xiàn)Linux下守護(hù)進(jìn)程的編寫方法
本文實(shí)例講述了Python實(shí)現(xiàn)Linux下守護(hù)進(jìn)程的編寫方法,分享給大家供大家參考,相信對于大家的Python程序設(shè)計會起到一定的幫助作用。具體方法如下:
1. 調(diào)用fork()以便父進(jìn)程可以退出,這樣就將控制權(quán)歸還給運(yùn)行你程序的命令行或shell程序。需要這一步以便保證新進(jìn)程不是一個進(jìn)程組頭領(lǐng)進(jìn)程(process group leader)。下一步,‘setsid()',會因為你是進(jìn)程組頭領(lǐng)進(jìn)程而失敗。進(jìn)程調(diào)用fork函數(shù)時,操作系統(tǒng)會新建一個子進(jìn)程,它本質(zhì)上與父進(jìn)程完全相同。子進(jìn)程從父進(jìn)程繼承了多個值的拷貝,比如全局變量和環(huán)境變量。兩個進(jìn)程唯一的區(qū)別就是fork的返回值。child(子)進(jìn)程接收返回值為0,而父進(jìn)程接收子進(jìn)程的pid作為返回值。調(diào)用fork函數(shù)后,兩個進(jìn)程并發(fā)執(zhí)行同一個程序,首先執(zhí)行的是調(diào)用了fork之后的下一行代碼。父進(jìn)程和子進(jìn)程既并發(fā)執(zhí)行,又相互獨(dú)立;也就是說,它們是“異步執(zhí)行”的。
2. 調(diào)用‘setsid()' 以便成為一個進(jìn)程組和會話組的頭領(lǐng)進(jìn)程。由于一個控制終端與一個會話相關(guān)聯(lián),而且這個新會話還沒有獲得一個控制終端,我們的進(jìn)程沒有控制終端,這對于守護(hù)程序來說是一件好事。
3. 再次調(diào)用‘fork()'所以父進(jìn)程(會話組頭領(lǐng)進(jìn)程)可以退出。這意味著我們,一個非會話組頭領(lǐng)進(jìn)程永遠(yuǎn)不能重新獲得控制終端。
4. 調(diào)用‘chdir("/")'確認(rèn)我們的進(jìn)程不保持任何目錄于使用狀態(tài)。不做這個會導(dǎo)致系統(tǒng)管理員不能卸裝(umount)一個文件系統(tǒng),因為它是我們的當(dāng)前工作目錄。 [類似的,我們可以改變當(dāng)前目錄至對于守護(hù)程序運(yùn)行重要的文件所在目錄]
5. 調(diào)用‘umask(0)'以便我們擁有對于我們寫的任何東西的完全控制。我們不知道我們繼承了什么樣的umask。 [這一步是可選的](譯者注:這里指步驟5,因為守護(hù)程序不一定需要寫文件)
6. 調(diào)用‘close()'關(guān)閉文件描述符0,1和2。這樣我們釋放了從父進(jìn)程繼承的標(biāo)準(zhǔn)輸入,標(biāo)準(zhǔn)輸出,和標(biāo)準(zhǔn)錯誤輸出。我們沒辦法知道這些文描述符符可能已經(jīng)被重定向去哪里。注意到許多守護(hù)程序使用‘sysconf()'來確認(rèn)‘_SC_OPEN_MAX'的限制?!甠SC_OPEN_MAX'告訴你每個進(jìn)程能夠打開的最多文件數(shù)。然后使用一個循環(huán),守護(hù)程序可以關(guān)閉所有可能的文件描述符。你必須決定你需要做這個或不做。如果你認(rèn)為有可能有打開的文件描述符,你需要關(guān)閉它們,因為系統(tǒng)有一個同時打開文件數(shù)的限制。
7. 為標(biāo)準(zhǔn)輸入,標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯誤輸出建立新的文件描述符。即使你不打算使用它們,打開著它們不失為一個好主意。準(zhǔn)確操作這些描述符是基于各自愛好;比如說,如果你有一個日志文件,你可能希望把它作為標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯誤輸出打開,而把‘/dev/null'作為標(biāo)準(zhǔn)輸入打開;作為替代方法,你可以將‘/dev/console'作為標(biāo)準(zhǔn)錯誤輸出和/或標(biāo)準(zhǔn)輸出打開,而‘/dev/null'作為標(biāo)準(zhǔn)輸入,或者任何其它對你的守護(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í)例代碼,相信會有新的收獲。
- linux shell實(shí)現(xiàn)守護(hù)進(jìn)程腳本
- php守護(hù)進(jìn)程 加linux命令nohup實(shí)現(xiàn)任務(wù)每秒執(zhí)行一次
- 詳解Linux中的守護(hù)進(jìn)程
- linux下如何創(chuàng)建守護(hù)進(jìn)程的步驟
- C語言編寫Linux守護(hù)進(jìn)程實(shí)例
- Java實(shí)現(xiàn)Linux下雙守護(hù)進(jìn)程
- linux 守護(hù)進(jìn)程詳解及建立守護(hù)進(jìn)程
- Linux守護(hù)進(jìn)程的啟動方法
- linux下的守護(hù)進(jìn)程
- linux守護(hù)進(jìn)程服務(wù)daemon、nohup、systemd的區(qū)別
相關(guān)文章
淺談Python類里的__init__方法函數(shù),Python類的構(gòu)造函數(shù)
下面小編就為大家?guī)硪黄獪\談Python類里的__init__方法函數(shù),Python類的構(gòu)造函數(shù)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
Python將圖片批量從png格式轉(zhuǎn)換至WebP格式
最近因為工作需要去研究了下png的壓縮,發(fā)現(xiàn)轉(zhuǎn)換成webp格式可以小很多,下面給大家分享利用Python將圖片批量從png格式轉(zhuǎn)換至WebP格式的方法,下面來一起看看。2016-08-08
python中的一些類型轉(zhuǎn)換函數(shù)小結(jié)
python中的一些類型轉(zhuǎn)換函數(shù),供大家參考2013-02-02
python 用lambda函數(shù)替換for循環(huán)的方法
今天小編就為大家分享一篇python 用lambda函數(shù)替換for循環(huán)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06

