Created
February 18, 2013 03:51
-
-
Save harmy/4975015 to your computer and use it in GitHub Desktop.
Auto restart Service any time it crashes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| #coding=gbk | |
| ################################################################################ | |
| # guard.py | |
| # | |
| # Auto restart Service any time it crashes. | |
| # If the Servers is manually shut down, this script will terminate. | |
| # | |
| # usage:./guard.py scene | |
| ################################################################################ | |
| import os | |
| import sys | |
| import smtplib | |
| import email.mime.text | |
| import time | |
| HOST = 'smtp.gmail.com' | |
| PORT = 25 | |
| def sendEmailReport(): | |
| mail_username='hack4cn@gmail.com' | |
| mail_password='YOURPASSWD' | |
| from_addr = mail_username | |
| to_addrs=('hack4cn@gmail.com') | |
| smtp = smtplib.SMTP() | |
| print 'connecting ...' | |
| try: | |
| print smtp.connect(HOST,PORT) | |
| except: | |
| print 'CONNECT ERROR ****' | |
| smtp.starttls() | |
| try: | |
| print 'loginning ...' | |
| smtp.login(mail_username,mail_password) | |
| except: | |
| print 'LOGIN ERROR ****' | |
| msg = email.mime.text.MIMEText('傲视苍穹服务器于今天{0}:{1}:{2}发生一次宕机,请紧张一下!'.format(*time.localtime()[3:6])) | |
| msg['From'] = from_addr | |
| msg['To'] = ';'.join(to_addrs) | |
| msg['Subject']='5555555555555555555555555' | |
| smtp.sendmail(from_addr,to_addrs,msg.as_string()) | |
| smtp.quit() | |
| def collectCoreFiles(): | |
| if not os.path.exists('core'): | |
| return | |
| os.system("tar czvf ../core/core.tgz core") | |
| os.unlink("core") | |
| os.chdir("../core") | |
| os.system("boar ci") | |
| os.chdir("../bin") | |
| def main(): | |
| restart_delay = 5 | |
| if len(sys.argv) != 2: | |
| print "Usage: ./guard.py server" | |
| sys.exit(0) | |
| cmd = str(sys.argv[1]) | |
| while True: | |
| status = os.system("nohup %s" % cmd) | |
| if status == 0: | |
| break | |
| else: | |
| time.sleep(restart_delay) | |
| collectCoreFiles() | |
| sendEmailReport() | |
| if __name__ == "__main__": | |
| sys.exit(main()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment