欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python-linux 下获取文件修改时间并且与当前系统时间相减的秒数

程序员文章站 2022-07-15 17:13:01
...
# LOG:
#   built time 2018-08-15
#   coder:ly


# !/usr/bin/python
# -*- coding: UTF-8 -*-

import os
import psutil
import re
import commands
import time
import datetime
class Control_disk():
    def __init__(self):
        self.check_disk()
        #self.check_file()


    def check_file(self):
         dir_path="/tmp/zlog.error.log"
         if not os.path.isfile(dir_path):
             return 150
         mtime = os.stat(dir_path)
         file_time =time.strftime("%H:%M:%S",time.localtime(mtime.st_mtime))#文件修改时间
         local_time = time.strftime("%H:%M:%S",time.localtime(time.time()))
         starttime = datetime.datetime.strptime(file_time,"%H:%M:%S")
         endtime = datetime.datetime.strptime(local_time,"%H:%M:%S")
         #print (endtime-starttime).seconds
         return ((endtime-starttime).seconds)#相减的秒数差


    def check_disk(self):
        _,value_disk = commands.getstatusoutput("ps aux | grep streammanage |grep -v 'grep'")
        if not value_disk:
            return
        #print (value_disk)
        status_disk = self.check_file()
        if status_disk <30 :#小于30S
            _,ps_tmp = commands.getstatusoutput("ps -eo pid,command | grep streammanage | grep -v 'grep'")#查看streammanage进程
            if re.search(r"./streammanage",ps_tmp,re.I | re.M):#搜索返回字符串关键字
                stream_pid = re.sub(r"./streammanage.*$","",ps_tmp).replace(" ","")#替换为空字符,获取进程号
                os.system("kill "+str(stream_pid))#kill 掉进程
                #print stream_pid
                #print ps_tmp
        else:
            return


if __name__ == '__main__':
    Control_disk()