日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 守护程序检测进程是否存在_python创建守护进程的疑问

發布時間:2024/9/18 python 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 守护程序检测进程是否存在_python创建守护进程的疑问 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我自己寫了一個簡易的下載和文件執行的客戶端,如下

"""

省略若干代碼

"""

#執行下載函數

def do_script():

"""Start working"""

script_list = None

for mark in range(3):

try:

# get scripts

if not script_list:

script_list = get_scripts()

if script_list == None:

continue

for script in script_list:

command_exec = 'chmod a+x %s' % script

PyShell.new_progress(command_exec)

PyShell.new_progress('./%s' % script)

# return

except Exception as e:

logging.info(traceback.format_exc())

if mark == 2:

send_error_log(e)

#創建守護進程

def createDaemon():

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)

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())

#寫pid文件

if os.path.exists(pidfile):

os.remove(pidfile)

pid = str(os.getpid())

file(pidfile, 'w+').write("%s\n" % pid)

#為什么執行函數不能寫這里,寫這里會導致代碼無法執行到這里?

if __name__ == '__main__':

if platform.system() == "Linux":

createDaemon()

#必須寫到main里,才可以執行?

while 1:

do_script()

time.sleep(20)

else:

os._exit(0)

其中,為什么執行函數不能寫在守護進程函數里,而必須寫到main函數里呢?

難道是因為守護進程函數里無法再創建進程原因?

總結

以上是生活随笔為你收集整理的python 守护程序检测进程是否存在_python创建守护进程的疑问的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。