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

Python3判断shell下进程是否存在&&启动&&邮件通知

时间:2017-08-02 10:19:25      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:type   div   logs   success   pass   roc   rtp   执行   time   

判断进程是否存在

def isRunning(process_name):
    try:
        process = len(os.popen(ps aux | grep " + process_name + " | grep -v grep).readlines())
        if process >= 1:
            return True
        else:
            return False
    except:
        print("Check process ERROR!!!")
        return False

 

启动挂掉的进程

def startProcess(process_script):
    try:
        result_code = os.system(process_script)
        if result_code == 0:
            return True
        else:
            return False
    except:
        print("Process start Error!!!")
        return False

 

 由于脚本层层调用,如需发现异常一定要查看各种日志。。。。。

crontab 和 shell命令下会有各种环境变量不一致的问题。。。。。

 

绊倒过N次了。。。。今天又被绊了。。。。

 

例子:实现监控某个进程,如果进程挂掉,则启动进程。

并且接着发邮件通知。。。

#!/bin/env python3
# -*- coding: utf-8 -*-

from exchangelib import DELEGATE, Account, Credentials, Message, Mailbox, HTMLBody
import sys, time
import os


def log(logfile, content):
    f = open(logfile, a)
    f.write(time.strftime("\n%Y-%m-%d %H:%M:%S   ") + content)
    f.flush()
    f.close()


def Email(to, subject, body):
    creds = Credentials(
        username=‘xxxxxx,
        password=‘xxxxxx
    )
    account = Account(
        primary_smtp_address=‘xxxxxx,
        credentials=creds,
        autodiscover=True,
        access_type=DELEGATE
    )
    m = Message(
        account=account,
        subject=subject,
        body=HTMLBody(body),
        to_recipients = [Mailbox(email_address=to)]
    )
    m.send()


def isRunning(process_name):
    try:
        process = len(os.popen(ps aux | grep " + process_name + " | grep -v grep).readlines())
        if process >= 1:
            return True
        else:
            return False
    except:
        print("Check process ERROR!!!")
        return False


def startProcess(process_script):
    try:
        result_code = os.system(process_script)
        if result_code == 0:
            return True
        else:
            return False
    except:
        print("Process start Error!!!")
        return False


if __name__ == __main__:

    process_name = "spark-streaming"
    process_script = "/bin/bash /home/admin/agent/spark/streaming_start.sh"

    subject = "datacollect-1 spark-streaming ERROR"
    logfile = "/home/admin/bin/logfile.log"

    content = ""
    wrong_to = "zhzhang09@126.com"
    sleep = 1

    content = "There are %d arguments, They are %s" % (len(sys.argv), str(sys.argv))
    log(logfile, content)

    if len(sys.argv) == 3:
        user = sys.argv[1]
        to = sys.argv[2]
        log(logfile, content)
        time.sleep(sleep)
        isrunning = isRunning(process_name)
        if isrunning == False:
            content = "spark-streaming running ERROR \n"
            log(logfile, content)
            Email(to, subject, content)
            isstart = startProcess(process_script)
            time.sleep(sleep)
            if isstart == True:
                content += "spark-streaming start SUCCESS \n"
                log(logfile, content)
                Email(to, subject+" && start SUCCESS", content)
    else:
        log(logfile, "running ERROR")

脚本执行方法:

/usr/local/bin/python3 /home/admin/bin/sparkStreamingEmail.py zhzhang09@126.com zhzhang09@126.com

 

 

未完待续。。。

 

Python3判断shell下进程是否存在&&启动&&邮件通知

标签:type   div   logs   success   pass   roc   rtp   执行   time   

原文地址:http://www.cnblogs.com/zhzhang/p/7271717.html

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