前面写过bash shell对nginx日志的切割,现在用python进行其操作。
放在crontab里面让其每个月自动执行一次。
#!/usr/bin/python
#-*-coding:UTF-8-*-
'''
Created on 2011-11-17
切割Nginx的日志文件,重新生成一个log日志,nginx的进程号不变动
@author: jimingsong
'''
import time
import os
path = ['/usr/local/nginx/logs/', 'nginx.pid','access.log']
str = time.strftime('%Y%m%d%H%M%S')
tar_conn1 = "cd %s && /bin/mv %s %s && /bin/kill -USR1 `cat %s`" % (path[0],path[2],str,path[1])
tar_conn = "cd %s && /bin/tar czvf %s %s" % (path[0],target,path[2])
if os.system(tar_conn1) == 0:
print '切割成功', str
else:
print 'Error,操作失败'
放在crontab里面让其每个月自动执行一次。