Python腳本后臺(tái)運(yùn)行的幾種方式_第1頁(yè)
Python腳本后臺(tái)運(yùn)行的幾種方式_第2頁(yè)
Python腳本后臺(tái)運(yùn)行的幾種方式_第3頁(yè)
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、這篇文章主要介紹了 Python 腳本后臺(tái)運(yùn)行的幾種方式 ,linux 下后臺(tái)運(yùn)行、通過(guò) upstart 方式實(shí)現(xiàn)、通過(guò) bash 腳本實(shí)現(xiàn)、通過(guò) screen、 tmux 等方式實(shí)現(xiàn) ,需要的朋友可以參考下一個(gè)用 python 寫(xiě)的監(jiān)控腳本 test1.py,用 while True 方式一直運(yùn)行,在 ssh 遠(yuǎn)程(使用 putty 終端)時(shí)通過(guò)以下命令啟動(dòng)腳本:代碼如下 :python test1.py &現(xiàn)在腳本正常運(yùn)行,通過(guò) ps能看到進(jìn)程號(hào),此時(shí)直接關(guān)閉 ssh終端(不是用 exit 命令, 是直接通過(guò) putty 的關(guān)閉按鈕執(zhí)行的) , 再次登錄后發(fā)現(xiàn)進(jìn)程已經(jīng)退出了。通過(guò)后

2、臺(tái)啟動(dòng)的方式該問(wèn)題已經(jīng)解決,這里總結(jié)下,也方便我以后查閱。linux 下后臺(tái)運(yùn)行通過(guò) fork 實(shí)現(xiàn)linux 環(huán)境下, 在 c 中守護(hù)進(jìn)程是通過(guò) fork 方式實(shí)現(xiàn)的, python 也可以通過(guò)該方式實(shí)現(xiàn), 示例代碼如下:代碼如下 :#!/usr/bin/env pythonimport time,platformimport osdef funzioneDemo():# 這是具體業(yè)務(wù)函數(shù)示例fout = open(/tmp/demone.log, w)while True:fout.write(time.ctime()+n)fout.flush()time.sleep(2)fout.clo

3、se()def createDaemon():# fork 進(jìn)程try:if os.fork() > 0: os._exit(0)except OSError, error:print fork #1 failed: %d (%s) % (error.errno, error.strerror)os._exit(1)os.chdir(/)os.setsid()os.umask(0)try:pid = os.fork()if pid > 0:print Daemon PID %d % pidos._exit(0)except OSError, error:print fork #2

4、failed: %d (%s) % (error.errno, error.strerror)os._exit(1)# 重定向標(biāo)準(zhǔn) IO sys.stdout.flush() sys.stderr.flush() si = file(/dev/null, r) so = file(/dev/null, a+) se = file(/dev/null, a+, 0) os.dup2(si.fileno(), sys.stdin.fileno() os.dup2(so.fileno(), sys.stdout.fileno() os.dup2(se.fileno(), sys.stderr.fil

5、eno()# 在子進(jìn)程中執(zhí)行代碼 funzioneDemo() # function demo if _name_ = _main_:if platform.system() = Linux: createDaemon() else: os._exit(0) 通過(guò) upstart 方式實(shí)現(xiàn) 可以通過(guò) upstart 把應(yīng)用封裝成系統(tǒng)服務(wù),這里直接記錄下完整示例。1、編寫(xiě) python 腳本 代碼如下 :rootlocal t27# cat test123.py #!/usr/bin/env python import os,time while True : print time.time()

6、 time.sleep(1)2、編寫(xiě) upstat 配置文件 代碼如下 :rootlocal t27# cat /etc/init/mikeTest.conf description My test author start on runlevel 234 stop on runlevel 0156 chdir /test/t27 exec /test/t27/test123.py respawn3、重新加載 upstate 代碼如下 : initctl reload-configuration4、啟動(dòng)服務(wù) 代碼如下 : rootlocal t27# start mikeTestmikeTes

7、t start/running, process 6635 rootlocal t27# ps aux | grep test123.pyroot 6635 0.0 0.0 22448 3716 ? Ss 09:55 0:00 python /test/t27/test123.py root 6677 0.0 0.0 103212 752 pts/1 S+ 09:56 0:00 grep test123.py5、停止服務(wù) 代碼如下 :rootlocal t27# stop mikeTest mikeTest stop/waitingrootlocal t27# ps aux | grep te

8、st123.pyroot 6696 0.0 0.0 103212 752 pts/1 S+ 09:56 0:00 grep test123.py rootlocal t27# 通過(guò) bash 腳本實(shí)現(xiàn)1、python 代碼 代碼如下 : rootlocal test# cat test123.py #!/usr/bin/env python import os,time while True : print time.time() time.sleep(1)2、編寫(xiě)啟動(dòng)腳本 代碼如下 :rootlocal test# cat start.sh#! /bin/shpython test123.p

9、y &3、啟動(dòng)進(jìn)程 代碼如下 : rootlocal test#./start.sh 如果直接用 & 啟動(dòng)進(jìn)程 : 代碼如下 :python test123.py &tmux 啟動(dòng)的方式。直接關(guān)閉 ssh 終端會(huì)導(dǎo)致進(jìn)程退出。 通過(guò) screen、 tmux 等方式實(shí)現(xiàn) 如果臨時(shí)跑程序的話, 可以通過(guò) screen、tmux 啟動(dòng)程序, 這里描述下1、啟動(dòng) tmux在終端輸入 tmux 即可啟動(dòng)2、在 tmux 中啟動(dòng)程序 直接執(zhí)行如下命令即可(腳本參考上面的) : python test123.py3、直接關(guān)閉 ssh終端(比如 putty 上的關(guān)閉按鈕) ;4、重新 ssh 上去之后,執(zhí)行如下命令:

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論