python 守护进程脚本

#!/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 守护进程脚本)