python写守护进程代码示例,python守护进程,下面是一个简单的pyth
python写守护进程代码示例,python守护进程,下面是一个简单的pyth
下面是一个简单的python守护进程的例子。实现监控邮箱是否超过容量,如果超限则发邮件的功能。
################################################################################################################### import os import sys class Daemonize: """ 创建守护进程的基类 """ def daemonize(self): try: #this process would create a parent and a child pid = os.fork() if pid > 0: # take care of the first parent sys.exit(0) except OSError, err: sys.stderr.write("Fork 1 has failed --> %d--[%s]\n" % (err.errno, err.strerror)) sys.exit(1) #change to root os.chdir('/') #detach from terminal os.setsid() # file to be created ? os.umask(0) try: # this process creates a parent and a child pid = os.fork() if pid > 0: print "Daemon process pid %d" % pid #bam sys.exit(0) except OSError, err: sys.stderr.write("Fork 2 has failed --> %d--[%s]\n" % (err.errno, err.strerror)) sys.exit(1) sys.stdout.flush() sys.stderr.flush() def start_daemon(self): self.daemonize() self.run_daemon() def run_daemon(self): """override""" pass################################################################################################################### from Daemonize import Daemonize from email.MIMEText import MIMEText import os import smtplib from smtplib import SMTPException import time class WatchFile(Daemonize): def __init__(self, file_path, size_limit=15728640): self.file = os.path.realpath(file_path) print '---' assert os.path.isfile(self.file), '%s does not exist' % self.file print '+++' self.userhome = os.getenv('HOME') self.smtpserver = '*your-host*' self.recipient_list = ['recipient@domain'] self.sender = 'sender@domain' self.file_size_limit = size_limit self.email_body = os.path.join(self.userhome, 'email-msg.txt') self.interval = 3600 self.log_file = os.path.join(self.userhome, 'inboxlog.txt') def send_an_email(self): """Method to send email to the recipients""" email_body = open(self.email_body, 'r').read() msg = MIMEText(email_body) msg['Subject'] = 'Your email inbox has exceeded size !' msg['From'] = 'Inbox WatchDog' msg['Reply-to'] = None msg['To'] = self.recipient_list session_obj = smtplib.SMTP() session_obj.connect(self.smtpserver) try: session_obj.sendmail(self.sender, self.recipient_list, msg.as_string()) except SMTPException: print "Unable to send emails." finally: session_obj.close() def watch(self): """Method to watch your inbox. This also logs the time when your inbox was last checked.""" current_file_size = os.path.getsize(self.file) if current_file_size > self.file_size_limit: self.send_an_email() f = open(self.log_file, 'a') f.write('Last checked on : %s' % time.asctime(time.localtime(time.time()))) f.write('\n') f.close() def run_daemon(self): """Over ridden method from Daemonize.This starts the daemon.""" while 1: self.watch() time.sleep(self.interval) if __name__ == '__main__': watchdog = WatchFile('path-to-your-inbox') watchdog.start_daemon()
上面实现的功能非常简单,只是为了说明下如何用python写守护进程。有了这个,相信其他复杂的守护进程你也可以搞定了。
相关内容
- 检测UDP端口是否开放,检测UDP端口开放,[Python]代码im
- python 各种解析xml包使用方法总结,pythonxml,使用python开
- python写日志的封装类,python日志封装,# encoding:u
- python两种生成md5的方法,python生成md5,一. 使用md5包imp
- python多线程下载网页,python多线程网页,多线程下载文件
- python 使用xml.dom.minidom解析xml,,如下代码展示如何使用
- python使用decorator做异常处理,pythondecorator,如下代码使用
- python gzip压缩文件,pythongzip,下面的代码实现用gzip
- python导出linux用户权限,pythonlinux,要换linux服务器,由
- python目录遍历代码,python目录,如下python实现目录
评论关闭