清理web服务器磁盘脚本,shell,py,初学python。

环境:web服务器,没装别的程序,基本占磁盘的就是nginx log文件


思路:当磁盘超过80%,删除3天之前的nginx日志

shell:

#!/bin/bash

disk_use=`df -h|awk '/\/$/ {a=+$(NF-1);if(a>80)print a}'`

ls /usr/local|grep tengine && PATH=/usr/local/tengine/logs || PATH=/usr/local/nginx/logs

if [ $disk_use ]

then

/usr/bin/find $PATH -name '*[0-9]*log' -mtime +3|/usr/bin/xargs /bin/rm -rf

fi


python:

#!/usr/bin/python

import commands

import os

import time

disk_use=int(commands.getoutput('df -h|grep -w "/"').split()[-2].split('%')[0])

app_list=os.listdir('/usr/local')


if "tengine" in app_list:

    log_dir="/usr/local/tengine/logs/"

else:

    log_dir="/usr/local/nginx/logs/"


if disk_use > 80:

    log_list=os.listdir(log_dir)

    time_now=time.strftime('%m-%d',time.localtime())

    for log in log_list:

        log_name=log_dir+log

        time_log=time.strftime('%m-%d',time.localtime(os.path.getmtime(log_name)))

        if time_log.split('-')[0] == time_now.split('-')[0] and int(time_now.split('-')[1])-int(time_log.split('-')[1])>3:

            os.remove(log_name)


你可能感兴趣的:(shell,python,清理磁盘)