python-linux 下獲取文件修改時間並且與當前系統時間相減的秒數

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章