监控文件是否更新

shell脚本:

#! /bin/sh

date=$(date +%Y-%m-%d-%H:%M)

file_path=/root/airflow/scheduler.log
check_time=120
file_old_stat="`stat ${file_path}|grep Size`"
sleep ${check_time}
file_new_stat="`stat ${file_path}|grep Size`"
if [[ `echo ${file_old_stat}` == `echo ${file_new_stat}` ]]; then
. /data/venv/bin/activate && supervisorctl restart airflow_scheduler

echo "#${date},scheduler service restarted" >>/tmp/test/scheduler_hung.log

fi

定时任务:

*/2 * * * * /bin/sh /hongfeng/script/monitor_scheduler.sh >/dev/null 2>&1

python脚本:

# -*- coding: UTF-8 -*-
import os
import time
file='/root/airflow/scheduler.log'
atime=os.path.getatime(file) #输出最近访问时间1318921018.0
ctime=os.path.getctime(file) #输出文件创建时间
mtime=os.path.getmtime(file) #输出最近修改时间
ntime=time.time()
timed=ntime-mtime
timef="notime:%s, mtime:%s, timed:%d" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ntime)),time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ctime)), timed)
print(timef)

有些人,自甘堕落;有些人,即便身处深渊,也依旧笑容灿烂。
原文地址:https://www.cnblogs.com/hongfeng2019/p/11448116.html