Python Cut nginx Log script

Source: Internet
Author: User
Tags timedelta

#!/usr/bin/env python

#-*-coding:utf-8-*-

#Python Nginx cut log 1

Import subprocess

def mkdir ():

Subprocess.call (' mkdir-pv/usr/local/nginx/logs/$ (date-d "Yesterday" + "%Y")/$ (date-d "Yesterday" + "%m")/', shell= True)

def MV ():

Subprocess.call (' mv/usr/local/nginx/logs/access.log/usr/local/nginx/logs/$ (date-d "Yesterday" + "%Y")/$ (date-d " Yesterday "+"%m ")/access_$ (date-d" Yesterda

Y "+"%y%m%d "). Log ', shell=true)

Def Kill ():

PID = open ("/usr/local/nginx/logs/nginx.pid", "R")

f = Pid.read ()

f = F.strip ()

Pid.close ()

Kill = "Kill"

Kill_usage = "-USR1"

Subprocess.call ([kill,kill_usage,f])

def main ():

mkdir ()

MV ()

Kill ()

if __name__ = = "__main__":

Main ()

#!/usr/bin/env python

#-*-coding:utf-8-*-

#Python Nginx cut Log 2

Import time

Import OS

Import datetime

Path = ['/usr/local/nginx/logs ', ' access.log ', ' nginx.pid ']

Now_time = Datetime.datetime.now ()

Yes_time = Now_time + Datetime.timedelta (days=-1)

Yes_ymd = Yes_time.strftime ("%y%m%d")

Yes_year = Yes_time.strftime ("%Y")

Yes_month = Yes_time.strftime ("%m")

Command1 = "MKDIR-PV%s/%s/%s"% (path[0],yes_year,yes_month)

Command2 = "CD%s && mv%s%s/%s/%s/%s.log"% (PATH[0],PATH[1],PATH[0],YES_YEAR,YES_MONTH,YES_YMD)

Command3 = "Kill-usr1 ' cat%s/%s '"% (path[0],path[2])

If Os.system (command1) = = 0:

Print "Catalog generated successfully! "

Else

Print "Directory Generation failed! "

If Os.system (command2) = = 0:

Print "Log cut successfully!" "

Else

Print "Log cut failed! "

If Os.system (command3) = = 0:

print "Smooth restart Nginx success!" "

Else

print "Smooth restart Nginx failed!" "

#!/usr/bin/env python

#-*-coding:utf-8-*-

#Python Nginx cut Log 3

Import time

Import OS

Import datetime

Import Shutil

#path列表存入路径和文件

Path = ['/usr/local/nginx/logs ', ' access.log ', ' nginx.pid ']

Now_time = Datetime.datetime.now () #现在时间

Yes_time = Now_time + Datetime.timedelta (days=-1) #昨天的时间

Yes_ymd = Yes_time.strftime ("%y%m%d") #格式化输出昨天的年月日

Yes_year = Yes_time.strftime ("%Y") #格式化输出昨天的年份

Yes_month = Yes_time.strftime ("%m") #格式化输出昨天的月份

#创建备份目录

Path_back = "%s/%s/%s/"% (path[0],yes_year,yes_month)

If os.path.exists (path_back) = = False:

Os.makedirs (Path_back)

#移动日志至备份目录下

File_now = "%s/%s"% (Path[0],path[1])

File_back = "%s/%s/%s/%s.log"% (PATH[0],YES_YEAR,YES_MONTH,YES_YMD)

Shutil.move (File_now,file_back)

#重置nginx日志

Pid_path = "%s/%s"% (path[0],path[2])

f = open (Pid_path)

PID = F.readline ()

PID = Pid.strip ()

Command = "KILL-USR1%s"% pid

Os.system (command)

F.close ()


This article is from the "My Ops Time" blog, so be sure to keep this source http://aaronsa.blog.51cto.com/5157083/1741507

Python Cut nginx Log script

Related Article

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.