purge_nohup_out.py
import os
import logging
import psutil
logging.basicConfig(
filename='app.log',
level=logging.INFO,
format='%(levelname)s:%(asctime)s:%(message)s'
)
def purge_nohup() :
for root, dirs, files in os.walk('/home/centos', topdown=False):
for name in files:
# print(name)
# 判断是否包含nohup
if "out" in name and "nohup" in name:
print(os.path.join(root, name))
with open(os.path.join(root, name),'w') as f:
print(os.stat(os.path.join(root, name)).st_size)
print(os.path.getsize(os.path.join(root, name)))
logging.info('the file %s is null ', os.path.join(root, name))
disk_usage=psutil.disk_usage('/')
print("硬盘根目录使用率为%s" % disk_usage.percent)
disk_usage=int(disk_usage.percent)
print("硬盘根目录使用率为%s" % disk_usage)
if disk_usage > 5:
purge_nohup()
网友评论