Python 写守护进程代码示例
实现的功能非常简单,只是为了说明下如何用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()
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。