zoukankan      html  css  js  c++  java
  • linux:使用python脚本监控某个进程是否存在(不使用crontab)

    背景:

      需要每天定时去检测crontab进程是否启动,所以不能用crontab来启动检测脚本了,直接使用while 循环和sleep方式实现定时检测

    # coding:utf-8
    import os
    import send_message
    import datetime
    import time
    
    
    curr_time = datetime.datetime.now()
    time_str = datetime.datetime.strftime(curr_time,'%H:%M:%S')
    
    def loopMonitor():
        curr_time = datetime.datetime.now()
        time_str = datetime.datetime.strftime(curr_time,'%H:%M')
    
        if time_str == '08:05':
            print 'come in already'
            check_process()
            return
        #2s检查一次
        time.sleep(60)
        loopMonitor()
    
    
    def check_process():
    
        process="./cron.lock"
    
        os.system("ps -ef|grep python3|grep -v grep >%s" % process)
        if not(os.path.getsize(process)):
            send_message.send_message('crontab not running', [8618320120156])
        else:
            f=open('./cron.lock', "r+")
            f.truncate()
    
    
    if __name__ == '__main__':
        loopMonitor()
  • 相关阅读:
    性能分析
    单例模式
    装饰者模式
    开发Chrome Extension截取你微博的帐号密码
    201703-3 markdown
    201612-3 权限查询
    201609-3 炉石传说
    201604-2 俄罗斯方块
    201604-3 路径解析
    201512-1 数位之和
  • 原文地址:https://www.cnblogs.com/gcgc/p/11471448.html
Copyright © 2011-2022 走看看