Python3判斷shell下進程是否存在&&啟動&&郵件通知

來源:互聯網
上載者:User

標籤: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, HTMLBodyimport sys, timeimport osdef 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 Falsedef 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 Falseif __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下進程是否存在&&啟動&&郵件通知

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.