清理web伺服器磁碟指令碼,shell,py,初學python。

來源:互聯網
上載者:User

標籤:清理磁碟   shell   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)


本文出自 “9400142” 部落格,請務必保留此出處http://9410142.blog.51cto.com/9400142/1688026

清理web伺服器磁碟指令碼,shell,py,初學python。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.