python管理linux的进程

#!的/ usr / bin中/蟒蛇
#coding = utf-8
导入系统
进口口
进口时间
进口信号

Max_process = 2
log_index = 0


def start():
    全局Max_process
    全局log_index

    #获取当前路径
    path = os.popen('pwd')。readlines()[0]

    而(1):
        processInfo = os.popen('ps -ef | grep tt.py | grep -v grep')。readlines()
        processNum = len(processInfo)
    if(processNum):
        os.system('cd'+路径)
        os.system('nohup python tt.py&')
        log_index = log_index + 1
        time.sleep(5)


def stopChicd():
    processInfo = os.popen(“ps -ef | grep tt.py | grep -v grep | awk'{print $ 2}'”)。readlines()
    for processInfo中的pid:
        os.kill(int(pid),signal.SIGKILL)


def stopParent():
    parentList = os.popen(“ps -ef | grep Monitor.py | grep -v grep | awk'{print $ 2}'”)。readlines()
    对于parentList中的pid:

        os.kill(int(pid),signal.SIGKILL)


#杀掉除了自己所有的进程
def stopParent2():
    index = 0
    parentList = os.popen(“ps -ef | grep Monitor.py | grep -v grep | awk'{print $ 2}'”)。readlines()
    对于parentList中的pid:
        index = index + 1
    如果索引:
        os.kill(int(pid),signal.SIGKILL)


def stopAll():
    stopChicd()
    stopParent()


尝试:
    fun = sys.argv [1]
除例外:
    有趣=''
if(fun =='start'):
    开始()
if(fun =='stop'):
    STOPALL()
if(fun =='restart'):
    stopChicd()
    stopParent2()
    time.sleep(1)
    开始()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值