Python腳本后臺(tái)運(yùn)行的幾種方式
一個(gè)用python寫的監(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ò)后臺(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 python
import time,platform
import os
def funzioneDemo():
# 這是具體業(yè)務(wù)函數(shù)示例
fout = open('/tmp/demone.log', 'w')
while True:
fout.write(time.ctime()+'\n')
fout.flush()
time.sleep(2)
fout.close()
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' % pid
os._exit(0)
except OSError, error:
print 'fork #2 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.fileno())
# 在子進(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、編寫python腳本
[root@local t27]# cat test123.py
#!/usr/bin/env python
import os,time
while True :
print time.time()
time.sleep(1)
2、編寫upstat配置文件
[root@local t27]# cat /etc/init/mikeTest.conf
description "My test"
author "Mike_Zhang@live.com"
start on runlevel [234]
stop on runlevel [0156]
chdir /test/t27
exec /test/t27/test123.py
respawn
3、重新加載upstate
initctl reload-configuration
4、啟動(dòng)服務(wù)
[root@local t27]# start mikeTest
mikeTest start/running, process 6635
[root@local t27]# ps aux | grep test123.py
root 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.py
5、停止服務(wù)
[root@local t27]# stop mikeTest
mikeTest stop/waiting
[root@local t27]# ps aux | grep test123.py
root 6696 0.0 0.0 103212 752 pts/1 S+ 09:56 0:00 grep test123.py
[root@local t27]#
通過(guò)bash腳本實(shí)現(xiàn)
1、python代碼
[root@local test]# cat test123.py
#!/usr/bin/env python
import os,time
while True :
print time.time()
time.sleep(1)
2、編寫啟動(dòng)腳本
[root@local test]# cat start.sh
#! /bin/sh
python test123.py &
3、啟動(dòng)進(jìn)程
[root@local test]#./start.sh
如果直接用&啟動(dòng)進(jìn)程:
python test123.py &
直接關(guān)閉ssh終端會(huì)導(dǎo)致進(jìn)程退出。
通過(guò)screen、tmux等方式實(shí)現(xiàn)
如果臨時(shí)跑程序的話,可以通過(guò)screen、tmux啟動(dòng)程序,這里描述下tmux啟動(dòng)的方式。
1、啟動(dòng)tmux
在終端輸入tmux即可啟動(dòng)
2、在tmux中啟動(dòng)程序
直接執(zhí)行如下命令即可(腳本參考上面的): python test123.py
3、直接關(guān)閉ssh終端(比如putty上的關(guān)閉按鈕);
4、重新ssh上去之后,執(zhí)行如下命令:
tmux attach
現(xiàn)在可以看到python程序還在正常執(zhí)行。
windows下后臺(tái)運(yùn)行
在windows下沒(méi)有深入的研究過(guò),我經(jīng)常用的方法是修改python腳本的擴(kuò)展名為".pyw",雙擊即可后臺(tái)運(yùn)行,不需要修改任何代碼。
相關(guān)文章
簡(jiǎn)介JavaScript中的getUTCFullYear()方法的使用
這篇文章主要介紹了簡(jiǎn)介JavaScript中的getUTCFullYear()方法的使用,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-06-06詳解JavaScript函數(shù)callee、call、apply的區(qū)別
這篇文章主要介紹了JavaScript函數(shù)callee、call、apply的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03三張圖帶你搞懂JavaScript的原型對(duì)象與原型鏈
這篇文章介紹了JavaScript的原型對(duì)象與原型鏈,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07用JS實(shí)現(xiàn)一個(gè)TreeMenu效果分享
用JS實(shí)現(xiàn)一個(gè)TreeMenu效果分享,思路比較簡(jiǎn)單,但很實(shí)用2011-08-08JavaScript中setTimeout和setInterval函數(shù)的傳參及調(diào)用
這篇文章主要介紹了JavaScript中setTimeout和setInterval函數(shù)的傳參及調(diào)用,著兩個(gè)函數(shù)可以把要執(zhí)行的代碼在設(shè)定的一個(gè)時(shí)間點(diǎn)插入js引擎維護(hù)的一個(gè)代碼隊(duì)列中,需要的朋友可以參考下2016-03-03