Python定时执行脚本
最近测试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定时执行脚本的更多相关文章
- Linux下定时执行脚本(转自Decode360)
文章来自:http://www.blogjava.net/decode360/archive/2009/09/18/287743.html Decode360's Blog 老师(业精于勤而荒于嬉 ...
- php定时执行脚本
php定时执行脚本 ignore_user_abort(); // run script. in background set_time_limit(0); // run script. foreve ...
- linux下使用crontab定时执行脚本
使用crontab定时执行脚本 cron服务是一个定时执行的服务,可以通过crontab 命令添加或者编辑需要定时执行的任务: crontab –e : 修改 crontab 文件,如果文件不存在会自 ...
- 实用脚本 2 -- Linux下定时执行脚本
今天学习Linux下定时执行脚本,看到一篇讲解比较好的文章,特此拿来分享. 原文链接:http://www.blogjava.net/decode360/archive/2009/09/18/2877 ...
- Linux-crontab定时执行脚本配置
crontab是一个可以根据自己配置的命令定时执行脚本的服务 安装crontab(centos) yum install Vixie-cron yum install crontabs vixie-c ...
- windows每天定时执行脚本
windows每天定时执行脚本 这里说的定时器就是Windows下的任务计划,当时遇到的坑正好总结一下,因为Windows10的定时器去执行脚本当时试了好多遍,都是没有成功,后来通过自己的观察发现是 ...
- python定时执行详解
知识点 1. sched模块,准确的说,它是一个调度(延时处理机制),每次想要定时执行某任务都必须写入一个调度. (1)生成调度器:s = sched.scheduler(time.time,time ...
- python自动化执行脚本
---恢复内容开始--- 1 (1)首先在你的.py文件上加上一行代码注释: #!/usr/local/bin/python2.7 (2)终端下执行: crontab -e 进入后,输入i 进入可编辑 ...
- crontab 定时执行脚本出错,但手动执行脚本正常
原因: crontab 没有去读环境变量,需要再脚本中手动引入环境变量,可以用source 也可以用export 写死环境变量. 为了定时监控Linux系统CPU.内存.负载的使用情况,写了个Shel ...
随机推荐
- LuaStudio编辑调试软件
该编辑调试器最大特点就是能够注入到宿主程序内对lua脚本进行调试.还能够设置断点观察变量的值,功能很强大. 如今已有的编辑器有Notepad++, Editplus, luaforwindows, S ...
- rac_安装软件时报版本号过高问题
原创作品,出自 "深蓝的blog" 博客.欢迎转载,转载时请务必注明下面出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...
- iOS机型适配
机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了,表示机器屏幕尺寸变大了: 像素:表示屏幕图片的大小,跟坐标之间有个对应关系,比如1:1或1:2等: ppi:代表屏幕物理大小到图片大小的 ...
- 谈谈 OC 中的内联函数
内联函数 即在编译的时候将函数体替换函数调用.从而不须要将parameter,returnaddress进行push/pop stack的操作, 从而加速app的执行.然而,会添加二进制文件的大小. ...
- Docker经常使用命令
Usage: docker [OPTIONS] COMMAND [arg...] -H=[unix:///var/run/docker.sock]: tcp://host:port to bind/ ...
- 设备树API
引子 首先看一个例子,也可参考linux设备树语法中的gpio示例.该示例选自openwrt的gpio-button-hotblug驱动. 设备树code: gpio-keys-polled { co ...
- 日志系统之扩展Flume-LineDeserializer
本人博客文章如未特别注明皆为原创.如有转载请注明出处:http://blog.csdn.net/yanghua_kobe/article/details/46595401 继续闲聊日志系统,在之前的博 ...
- X264参考手册
艺搜简介 基本语法: x264 [options]-o outfile infile 注意与ffmpeg的输入输出文件位置恰好相反: ffmpeg[options][[infile options]- ...
- 苯(Benzene)
在常温下是甜味.可燃.有致癌毒性的无色透明液体,其密度小于水,但分子质量大于水,并带有强烈的芳香气味.它难溶于水,易溶于有机溶剂,本身也可作为有机溶剂.苯是一种石油化工基本原料,其产量和生产的技术水平 ...
- 集合Map多对多映射(使用xml文件)
我们可以使用set,bag,map等来映射多对多关系.在这里,我们将使用map来进行多对多映射. 在这种情况下,将创建三个表. 多对多映射示例 我们需要创建以下文件来映射map元素.首先创建一个项目: ...