当前位置:   article > 正文

python启停 linux进程,Python守护进程以及命令行启动停止

linux停止python命令

#!/usr/bin/python

#coding=utf-8

import sys

import os

import time

import signal

Max_process = 2

log_index = 0

def start():

global Max_process

global log_index

#获取当前路径

path = os.popen('pwd').readlines()[0]

while(1):

processInfo = os.popen('ps -ef | grep tt.py | grep -v grep').readlines()

processNum = len(processInfo)

if(processNum

os.system('cd '+path)

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 pid in processInfo:

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

def stopParent():

parentList = os.popen("ps -ef|grep Monitor.py|grep -v grep|awk '{print $2}'").readlines()

for pid in parentList:

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

for pid in parentList:

index = index+1

if(index

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

def stopAll():

stopChicd()

stopParent()

try:

fun = sys.argv[1]

except Exception:

fun = ''

if(fun == 'start'):

start()

if(fun == 'stop'):

stopAll()

if(fun == 'restart'):

stopChicd()

stopParent2()

time.sleep(1)

start()

调用的时候 python Monitor.py start|stop|restart即可

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/72318
推荐阅读
相关标签
  

闽ICP备14008679号