美文网首页
python 守护进程脚本

python 守护进程脚本

作者: 乔伊斯 | 来源:发表于2016-07-22 14:39 被阅读0次

    #!/usr/bin/env python

    # -*- coding: utf-8 -*-

    import time

    import sys

    import os

    import time

    import smtplib

    from email.MIMEText import MIMEText

    mail_host="smtp.qq.com"

    mail_user="1020121001"

    mail_pass="1020121001"

    mail_postfix="qq.com"

    def send_mail(to_list,sub,content):

    me=mail_user+"<"+mail_user+"@"+mail_postfix+">"

    msg = MIMEText(content)

    msg['Subject'] = sub

    msg['From'] = me

    msg['To'] = ";".join(to_list)

    try:

    s = smtplib.SMTP()

    s.connect(mail_host)

    s.login(mail_user,mail_pass)

    s.sendmail(me, to_list, msg.as_string())

    s.close()

    return True

    except Exception, e:

    print str(e)

    return False

    def autohalt():

    while True:

    ps_string = os.popen('ps -ax | grep '+sys.argv[1]+'|grep -v grep  ','r').read()

    ps_strings = ps_string.strip().split('\n')

    if len(ps_strings) < 2:

    mailto_list=["88376827@qq.com","joyesong@qq.com"]

    mail_sub='process '+sys.argv[1]+' can not find ...'

    local_time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

    mail_content=mail_sub+' '+local_time

    send_mail(mailto_list,mail_sub,mail_content)

    os.system("service zabbix-agent start")

    #return

    else:

    print 'Still',len(ps_strings)-1,'Processes, waiting 10s...'

    time.sleep(10)

    if __name__=='__main__':

    autohalt()

    相关文章

      网友评论

          本文标题:python 守护进程脚本

          本文链接:https://www.haomeiwen.com/subject/lxeijttx.html