最近测试hbase,老发现服务挂掉,自己不能及时发现,想了想,写了个脚本,让脚本每个小时执行一次,以便检测是否有服务挂掉,如果有服务挂掉,及时启动

import os
import datetime
class CheckService(object): def __init__(self):
pass
  
def getService(self):
# service list
service = ['Jps'
,'EmbeddedServer'
,'QuorumPeerMain'
,'NodeManager'
,'ResourceManager'
,'NameNode'
,'SecondaryNameNode'
,'HMaster'
,'HRegionServer'
,'RunJar' ]
# Traversal list
for i in service:
a = "jps | awk '{print $2}'|grep "+i
res = os.system(a)
# Determine whether the service is running
if res != 0 :
print "%s is not running!" %(i)
if i == 'HMaster':
os.system('start-hbase.sh')
elif i == 'HRegionServer':
os.system('local-regionservers.sh start 1')
elif i == 'EmbeddedServer':
os.system('ranger-admin start')
elif i == 'QuorumPeerMain':
os.system('zkServer.sh start')
else:
print 'restart Hadoop !!!' def timerFun(self,sched_Timer):
flag = 0
while True:
now = datetime.datetime.now()
if now == sched_Timer:
self.getService()
flag = 1
         
else:
if flag == 1:
sched_Timer = sched_Timer + datetime.timedelta(hours=1)
flag = 0 if __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)

这个代码也可以扩展,把hours=1改成minutes=1就变成了每个小时定时任务,改成days=1就变成每天的定时任务

Python定时执行脚本的更多相关文章

  1. Linux下定时执行脚本(转自Decode360)

    文章来自:http://www.blogjava.net/decode360/archive/2009/09/18/287743.html Decode360's Blog  老师(业精于勤而荒于嬉 ...

  2. php定时执行脚本

    php定时执行脚本 ignore_user_abort(); // run script. in background set_time_limit(0); // run script. foreve ...

  3. linux下使用crontab定时执行脚本

    使用crontab定时执行脚本 cron服务是一个定时执行的服务,可以通过crontab 命令添加或者编辑需要定时执行的任务: crontab –e : 修改 crontab 文件,如果文件不存在会自 ...

  4. 实用脚本 2 -- Linux下定时执行脚本

    今天学习Linux下定时执行脚本,看到一篇讲解比较好的文章,特此拿来分享. 原文链接:http://www.blogjava.net/decode360/archive/2009/09/18/2877 ...

  5. Linux-crontab定时执行脚本配置

    crontab是一个可以根据自己配置的命令定时执行脚本的服务 安装crontab(centos) yum install Vixie-cron yum install crontabs vixie-c ...

  6. windows每天定时执行脚本

     windows每天定时执行脚本 这里说的定时器就是Windows下的任务计划,当时遇到的坑正好总结一下,因为Windows10的定时器去执行脚本当时试了好多遍,都是没有成功,后来通过自己的观察发现是 ...

  7. python定时执行详解

    知识点 1. sched模块,准确的说,它是一个调度(延时处理机制),每次想要定时执行某任务都必须写入一个调度. (1)生成调度器:s = sched.scheduler(time.time,time ...

  8. python自动化执行脚本

    ---恢复内容开始--- 1 (1)首先在你的.py文件上加上一行代码注释: #!/usr/local/bin/python2.7 (2)终端下执行: crontab -e 进入后,输入i 进入可编辑 ...

  9. crontab 定时执行脚本出错,但手动执行脚本正常

    原因: crontab 没有去读环境变量,需要再脚本中手动引入环境变量,可以用source 也可以用export 写死环境变量. 为了定时监控Linux系统CPU.内存.负载的使用情况,写了个Shel ...

随机推荐

  1. C语言学习笔记(四) 流程控制

    流程控制 流程控制,说通俗一点就是程序代码执行的顺序.不管对于哪门语言来说,流程控制都是很重要的一部分内容: 流程控制的分类,可以分为三大类: 1.顺序 这个很好理解,顺序执行就是代码从上往下一行行的 ...

  2. mysql 主库有数据通过锁库做主从

    master@localhost[(none)]> grant replication slave on *.* to 'repl'@'192.168.1.177' identified by ...

  3. Spring Boot(五):Spring Boot的启动器Starter大全及自定义Starter

    现有启动器Starter目录 Spring Boot应用启动器基本的一共有44种,具体如下: 1)spring-boot-starter 这是Spring Boot的核心启动器,包含了自动配置.日志和 ...

  4. linux文件系统调用(1)---mount

    术语表: struct mount:挂载点 struct mountpoint:挂载点节点 struct vfsmount:挂载项 源文件系统:用户将要挂载的文件系统 目的文件系统:挂载源文件系统的文 ...

  5. 实时竞价(RTB) 介绍(基础篇)

    前言: 说到"实时竞价"大家一定都不陌生,那么为何如今实时竞价发展这么迅猛,当然这个主要得益于总体移动互联网环境的成熟,以及中国本地移动广告市场出现爆发式增长.那么到底什么是实时竞 ...

  6. error C2998: 'XXXXXXX' : cannot be a template definition 的可能原因。

    从错误信息的字面意思可以看出来是 XXXXX 不能作为模板定义. 但是为什么不能,并没有说明,最后我翻阅各种资料,各种尝试后,发现往往可能是由于找不到模板函数的某个参数的定义而导致的. templat ...

  7. 1211日课后shell总结

      1211shell   作者:高波 归档:学习笔记 2017年12月11日13:10:56 快捷键: Ctrl + 1 标题1 Ctrl + 2 标题2 Ctrl + 3 标题3 Ctrl + 4 ...

  8. 虚拟化笔记05 OpenFiler configuration

    5.OpenFiler configuration 登录Openfiler 步骤1:安装 FireFox 步骤2:通过FireFox以HTTPS://server-ip:446 远程访问OpenFil ...

  9. flask/sqlalchemy - OperationalError: (sqlite3.OperationalError) no such table

    狗书第五章 记得要先创建表 执行 db.create_all()语句来创建表 https://segmentfault.com/q/1010000005794140

  10. HTML 网页中以超链接的方式调用iphone 手机的app

    2011-11-13 14:36:33|  分类: 随笔 |  标签:iphone  调用iphone手机app  |举报|字号 订阅     <1>. 调用iphone 手机地图APP的 ...