Clean up Web server disk scripts, shell,py, beginner python.

Source: Internet
Author: User

Environment: Web server, no other program, the basic disk is the Nginx log file


Idea: When the disk exceeds 80%, delete the Nginx log 3 days ago

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)


This article is from the "9400142" blog, please be sure to keep this source http://9410142.blog.51cto.com/9400142/1688026

Clean up Web server disk scripts, shell,py, beginner python.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.