Python實(shí)現(xiàn)Linux下守護(hù)進(jìn)程的編寫(xiě)方法
本文實(shí)例講述了Python實(shí)現(xiàn)Linux下守護(hù)進(jìn)程的編寫(xiě)方法,分享給大家供大家參考,相信對(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ì)話(huà)組的頭領(lǐng)進(jìn)程。由于一個(gè)控制終端與一個(gè)會(huì)話(huà)相關(guān)聯(lián),而且這個(gè)新會(huì)話(huà)還沒(méi)有獲得一個(gè)控制終端,我們的進(jìn)程沒(méi)有控制終端,這對(duì)于守護(hù)程序來(lái)說(shuō)是一件好事。
3. 再次調(diào)用‘fork()'所以父進(jìn)程(會(huì)話(huà)組頭領(lǐng)進(jìn)程)可以退出。這意味著我們,一個(gè)非會(huì)話(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)前工作目錄。 [類(lèi)似的,我們可以改變當(dāng)前目錄至對(duì)于守護(hù)程序運(yùn)行重要的文件所在目錄](méi)
5. 調(diào)用‘umask(0)'以便我們擁有對(duì)于我們寫(xiě)的任何東西的完全控制。我們不知道我們繼承了什么樣的umask。 [這一步是可選的](譯者注:這里指步驟5,因?yàn)槭刈o(hù)程序不一定需要寫(xiě)文件)
6. 調(diào)用‘close()'關(guān)閉文件描述符0,1和2。這樣我們釋放了從父進(jìn)程繼承的標(biāo)準(zhǔn)輸入,標(biāo)準(zhǔn)輸出,和標(biāo)準(zhǔn)錯(cuò)誤輸出。我們沒(méi)辦法知道這些文描述符符可能已經(jīng)被重定向去哪里。注意到許多守護(hù)程序使用‘sysconf()'來(lái)確認(rèn)‘_SC_OPEN_MAX'的限制。‘_SC_OPEN_MAX'告訴你每個(gè)進(jìn)程能夠打開(kāi)的最多文件數(shù)。然后使用一個(gè)循環(huán),守護(hù)程序可以關(guān)閉所有可能的文件描述符。你必須決定你需要做這個(gè)或不做。如果你認(rèn)為有可能有打開(kāi)的文件描述符,你需要關(guān)閉它們,因?yàn)橄到y(tǒng)有一個(gè)同時(shí)打開(kāi)文件數(shù)的限制。
7. 為標(biāo)準(zhǔn)輸入,標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤輸出建立新的文件描述符。即使你不打算使用它們,打開(kāi)著它們不失為一個(gè)好主意。準(zhǔn)確操作這些描述符是基于各自愛(ài)好;比如說(shuō),如果你有一個(gè)日志文件,你可能希望把它作為標(biāo)準(zhǔn)輸出和標(biāo)準(zhǔn)錯(cuò)誤輸出打開(kāi),而把‘/dev/null'作為標(biāo)準(zhǔn)輸入打開(kāi);作為替代方法,你可以將‘/dev/console'作為標(biāo)準(zhǔn)錯(cuò)誤輸出和/或標(biāo)準(zhǔn)輸出打開(kāi),而‘/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ì)有新的收獲。
- 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語(yǔ)言編寫(xiě)Linux守護(hù)進(jìn)程實(shí)例
- Java實(shí)現(xiàn)Linux下雙守護(hù)進(jìn)程
- linux 守護(hù)進(jìn)程詳解及建立守護(hù)進(jìn)程
- Linux守護(hù)進(jìn)程的啟動(dòng)方法
- linux下的守護(hù)進(jìn)程
- linux守護(hù)進(jìn)程服務(wù)daemon、nohup、systemd的區(qū)別
相關(guān)文章
利用python/R語(yǔ)言繪制圣誕樹(shù)實(shí)例代碼
圣誕節(jié)快到了,分別用R和Python繪制了圣誕樹(shù)祝你們圣誕節(jié)快樂(lè),所以下面這篇文章主要給大家介紹了關(guān)于如何利用python/R繪制圣誕樹(shù)的相關(guān)資料,需要的朋友可以參考下2021-12-12
淺談Python類(lèi)里的__init__方法函數(shù),Python類(lèi)的構(gòu)造函數(shù)
下面小編就為大家?guī)?lái)一篇淺談Python類(lèi)里的__init__方法函數(shù),Python類(lèi)的構(gòu)造函數(shù)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
Python將圖片批量從png格式轉(zhuǎn)換至WebP格式
最近因?yàn)楣ぷ餍枰パ芯苛讼聀ng的壓縮,發(fā)現(xiàn)轉(zhuǎn)換成webp格式可以小很多,下面給大家分享利用Python將圖片批量從png格式轉(zhuǎn)換至WebP格式的方法,下面來(lái)一起看看。2016-08-08
python中的一些類(lèi)型轉(zhuǎn)換函數(shù)小結(jié)
python中的一些類(lèi)型轉(zhuǎn)換函數(shù),供大家參考2013-02-02
python 用lambda函數(shù)替換for循環(huán)的方法
今天小編就為大家分享一篇python 用lambda函數(shù)替換for循環(huán)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06

