码迷,mamicode.com
首页 > 编程语言 > 详细

Python通过fork的方式防止僵尸进程

时间:2018-01-11 22:25:09      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:span   python   err   system   else   force   ret   方式   *args   

import subprocess
import os
import sys
import platform


def fock_new(func):
    def inner(*args, **kwargs):
        if Linux.__eq__(platform.system()):
            print "main process start"
            try:
                pid = os.fork()
                if pid > 0:
                    print "after fock enter main process"
                    print {result: success, msg: Starting deploy}
                    sys.exit(0)
            except OSError, e:
                print e

            print "after fock enter sub process"
            try:
                os.setsid()
                pid = os.fork()
                if pid > 0:
                    print "after fock 2 enter sub process"
                    sys.exit(0)
            except OSError, e:
                print e
            print "after fock 2 enter sunzi process"
            return func(*args, **kwargs)
        else:
            return True
            return func(*args, **kwargs)
    return inner



def test(command):
    print fock_new(subprocess.Popen)(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)


if __name__ == __main__:
    url = "http://xxx.tar.gz"
    path = "E:/xiaol/T"
    command = "sh Download.sh {url} {path}".format(url=url, path=path)
    test(command)
#!/usr/bin/sh
echo $$>$2/ppid  && echo $1>>$2/download_url && wget -o $2/wget-log -P $2 $1 && echo Decompression....>>$2/wget-log && tar -zxf $2/*.tar.gz -C $2  --force-local && rm -rf $2/wget-log $2/ppid $2/*.tar.gz

 

Python通过fork的方式防止僵尸进程

标签:span   python   err   system   else   force   ret   方式   *args   

原文地址:https://www.cnblogs.com/413xiaol/p/8270501.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!