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.