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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM