Python timed script execution

Source: Internet
Author: User

Recently Test hbase, the old discovery service hangs, oneself can not find out in time, think, wrote a script, let the script execute once every hour, in order to detect whether there is a service hanging off, if there is a service hangs, timely start

ImportOSImportdatetimeclassCheckservice (object):def __init__(self):Pass        defGetService (self):#Service ListService = ['Jps'                ,'Embeddedserver'                ,'Quorumpeermain'                ,'NodeManager'                ,'ResourceManager'                ,'NameNode'                ,'Secondarynamenode'                ,'Hmaster'                ,'Hregionserver'                ,'Runjar'                ]        #Traversal list         forIinchservice:a="JPS | awk ' {print $} ' |grep"+I res=Os.system (a)#Determine whether the service is running            ifRes! =0:Print "%s is not running!"%(i)ifi = ='Hmaster': Os.system ('start-hbase.sh')                elifi = ='Hregionserver': Os.system ('local-regionservers.sh start 1')                elifi = ='Embeddedserver': Os.system ('ranger-admin Start')                elifi = ='Quorumpeermain': Os.system ('zkserver.sh Start')                    Else:                    Print 'Restart Hadoop!!!'    defTimerfun (Self,sched_timer): Flag=0 whileTrue:now=Datetime.datetime.now ()ifnow = =Sched_Timer:self.getService () flag= 1
          Else: ifFlag = = 1: Sched_timer= Sched_timer + Datetime.timedelta (hours=1) Flag=0if __name__=="__main__": CS=checkservice () Sched_timer= Datetime.datetime (2017,7,25,9,14) Print 'Run the timer task at {0}'. Format (sched_timer) cs.timerfun (Sched_timer)

This code can also be extended, change the hours=1 into Minutes=1 to become the hourly scheduled task, change to Days=1 become a daily scheduled task

Python timed script execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.