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

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

 

转载于:https://www.cnblogs.com/413xiaol/p/8270501.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值