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 ...
随机推荐
- 设计模式之简单工厂模式(C#语言描述)
严格意义上来说,简单工厂模式并不属于GoF的23种设计模式,但是它是学习其他工厂模式的基础和前提条件.理解了简单工厂模式,学习工厂方法模式和抽象工厂模式会比较容易一些. 简单工厂模式的定义 定义一个工 ...
- YII用户注冊表单的实现熟悉前台各个表单元素操作方式
模还是必须定义两个基本方法.还有部分label标签映射为汉字,假设进行表单验证,还要定义一些验证规则: <? php /* * 用户模型 * */ class user extends CAct ...
- 图片扩展---基于opencv-python实现
目标: 将一张长方形图片扩展成一张正方形图片,例如: 200x300x3的一张图片扩展成一张300x300x3的图片,填充部分使用白色. 代码: import cv2 import os imglis ...
- Android解决button反复点击问题
public class BaseActivity extends Activity { protected boolean isDestroy; //防止反复点击设置的标志.涉及到点击打开其它Act ...
- VS项目名称修改
阅读数:11141 VS中新建一个项目,如果开发工作都接近尾声,客户来要求更换项目的名称,差不多要变更整个解决方案中项目名称,引用等等,这个工作量还是很大的.上网搜索解决方法,还实验了专门的修改项目名 ...
- 类matlab find函数
逻辑矩阵,找出元素1并记录其位置索引. int main(int argc, char** argv) { unsigned ][] = { , , , , , , , , , , , , , , , ...
- 实战c++中的string系列--std::string与MFC中CString的转换
搞过MFC的人都知道cstring,给我们提供了非常多便利的方法. CString 是一种非常实用的数据类型. 它们非常大程度上简化了MFC中的很多操作,使得MFC在做字符串操作的时候方便了非常多.无 ...
- Spring MVC的@ResponseBody回来JSON串
1 406错误 <mvc:annotation-driven />不用动,请求的时候URL的文件扩展名应为json @ResponseBody会根据扩展名,或者Header,或者Param ...
- Ext扩展的QQ表情选择面板
Ext扩展的QQ表情选择面板 define(function () { EmoteChooser = function(cfg){ this.width=340; this.height=112; t ...
- Python Theano TypeError: Cannot convert Type TensorType(float64, vector) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(float64, matrix)
参考: https://groups.google.com/forum/#!topic/theano-users/teA-07wOFpE 这个问题出现的原因是,我在读文件的时候,应该Train_X读成 ...