標籤: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下進程是否存在&&啟動&&郵件通知