python监控日志并予以清理,python监控日志,Daemonize.py
python监控日志并予以清理,python监控日志,Daemonize.py
Daemonize.py
import osimport sysclass Daemonize: def daemonize(self): try: pid = os.fork() if pid > 0: sys.exit(0) except OSError,e: sys.stderr.write("Fork 1 has failed --> %d--[%s]\n" \ % (e.errno,e.strerror)) sys.exit(1) os.chdir('/') #detach from terminal os.setsid() #file to be created? os.umask(0) try: pid = os.fork() if pid > 0: print "Daemon process pid %d" % pid sys.exit(0) except OSError, e: sys.stderr.write("Fork 2 has failed --> %d--[%s]" \ % (e.errno, e.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
watchLog.py
import osimport smtplibfrom smtplib import SMTPExceptionimport timefrom Daemonize import Daemonizefrom subprocess import PopenLOG_PATH = ''LOG_FILENAME = ''class WatchLog(Daemonize): def __init__(self, file_path, size_limit=15728640): self.file = os.path.realpath(file_path) print self.file print '---' assert os.path.isfile(self.file), '%s does not exist' % self.file print '+++' self.smtpserver = "path to your host" self.recipient_list = ['@gmail.com'] self.sender = '@' self.file_size_limit = size_limit self.email_body = "path to your email tempalte" def send_an_email(self): email_body = open(self.email_body, 'r').read() session_obj = smtplib.SMTP(self.smtpserver) try: session_obj.sendmail(self.sender, self.recipient_list, email_body) except SMTPException: print "unable to send emails" finally: session_obj.close() def delFile(self): os.chdir(LOG_PATH) cmd = "echo '' > " + LOG_FILENAME try: Popen(cmd, shell=True) except Exception,e: print e def watch(self): current_file_size = os.path.getsize(self.file) if current_file_size > self.file_size_limit: self.defFile() def run_daemon(self): #override while True: self.watch() time.sleep(3600)if __name__ == "__main__": watchdog = WatchLog(LOG_PATH+LOG_FILENAME) watchdog.start_daemon()
相关内容
- 读取excel内容并写入sqlite中,excelsqlite,[Python]代码fn
- 扫描端口使用情况,扫描端口情况,[Python]代码#!
- subprocess 使用一例,subprocess使用,用subprocess实
- 增强亮度,,[Python]代码#
- 用途都在各个函数名称之前的注释上了,用途函数注释
- Python 读取纯真IP数据库,pythonip数据库,ip.py#!/usr/
- DiscuzRobot - Discuz!论坛的机器人程序,实现了登录,签到
- 调用Windows API锁定计算机,调用windowsapi锁定,[Python]代码
- 检测图片格式,,[Python]代码#
- 金山快盤自動簽到 python 3.2,動簽,[Python]代码#!
评论关闭