加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

python守护进程,以及命令行启动停止和重启

(2013-08-26 12:26:03)
标签:

python

守护进程

启动

关闭

重启

分类: 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即可

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有