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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python3判断shell下进程是否存在&&启动&&邮件通知相关的知识,希望对你有一定的参考价值。

判断进程是否存在

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 = "[email protected]"
    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 [email protected] [email protected]

 

 

未完待续。。。

 

以上是关于Python3判断shell下进程是否存在&&启动&&邮件通知的主要内容,如果未能解决你的问题,请参考以下文章

Shell 判断进程是否存在

grep在shell脚本中使用,判断进程是否存在

linux shell 判断进程是不是存在

shell脚本判断进程是不是存在,并重新启动

用shell脚本监控进程是否存在 不存在则启动的实例附带if判断详细条件

shell