欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python腳本后臺(tái)運(yùn)行的幾種方式

 更新時(shí)間:2015年03月09日 12:02:24   投稿:junjie  
這篇文章主要介紹了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寫的監(jiān)控腳本test1.py,用while True方式一直運(yùn)行,在ssh遠(yuǎn)程(使用putty終端)時(shí)通過(guò)以下命令啟動(dòng)腳本:

復(fù)制代碼 代碼如下:

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),示例代碼如下:

復(fù)制代碼 代碼如下:

#!/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腳本

復(fù)制代碼 代碼如下:

[root@local t27]# cat test123.py
#!/usr/bin/env python

import os,time

while True :
    print time.time()
    time.sleep(1)


2、編寫upstat配置文件
復(fù)制代碼 代碼如下:

[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
復(fù)制代碼 代碼如下:

initctl reload-configuration

4、啟動(dòng)服務(wù)
復(fù)制代碼 代碼如下:

[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ù)
復(fù)制代碼 代碼如下:

[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代碼

復(fù)制代碼 代碼如下:

[root@local test]# cat test123.py
#!/usr/bin/env python

import os,time

while True :
    print time.time()
    time.sleep(1)


2、編寫啟動(dòng)腳本
復(fù)制代碼 代碼如下:

[root@local test]# cat start.sh
#! /bin/sh

python test123.py &


3、啟動(dòng)進(jìn)程
復(fù)制代碼 代碼如下:

[root@local test]#./start.sh

如果直接用&啟動(dòng)進(jìn)程:
復(fù)制代碼 代碼如下:

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í)行如下命令:

復(fù)制代碼 代碼如下:

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)文章

最新評(píng)論