最近测试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. redis清空部分key

    redis-cli keys "test:job:*" redis-cli keys "test:job:*" |xargs redis-cli del

  2. 【LeetCode】two num 利用comparable接口 对对象进行排序

    题目two num 题意:给定一个整数数组和一个目标值.要求在数组中找到两个数.使得它们的和相加等于目标值.而且返回两个数的下标 思路:1.假设使用暴力,时间复杂度为O(n^2) 2.能够先将全部数进 ...

  3. 激活modelsim se 10.4 时运行patch_dll.bat不能生成TXT

    问题描述: 激活modelsim时运行patch_dll.bat总是在DOS界面一闪而过,不能生成LICENSE.TXT 问题解决: 先取消文件 mgls64.dll 的只读属性(这句话在README ...

  4. 237. Delete Node in a Linked List【easy】

    237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...

  5. centos配置IP地址

    1. vi /etc/sysconfig/network-scripts/ifcfg-eth0 2. DEVICE=eth1HWADDR=00:0C:29:D2:9A:F5TYPE=EthernetU ...

  6. go开启多进程——拆分多个进程同时处理(分而治之)

    使用了goroutine实现了多线程,使用chan来控制多线程. runtime.GOMAXPROCS(3)来设置最大的原生线程. runtime.Gosched() 显式地让出CPU时间给其他gor ...

  7. Spring MVC集成Log4j

    以下示例显示如何使用Spring Web MVC框架集成LOG4J.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: 创建一 ...

  8. Eclipse 运行配置(Run Configuration)

    Eclipse 运行配置(Run Configuration) 创建和使用 Eclipse 运行配置 在运行配置(Run Configuration)对话框中可以创建多个运行配置.每个配置可以在应用中 ...

  9. Openstack(Kilo)安装系列之环境准备(二)

    控制节点.网络节点.计算节点: 一.配置源 1.配置EPEL源 yum install http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-rel ...

  10. asp.net网站底部的版权信息实现代码且可维护

    网站底部的版权信息在特殊情况还是比较重要的所以在实现的时候一定要尽可能的做到可维护性,接下来将介绍一些技巧可达到可维护效果,感兴趣的你可不要错过了哈 一个大网站页面很多,如果每个版权信息直接写在下面, ...