#!/usr/bin/env pthon #_*_coding=utf-8_*_ #python版本(nginx日志切割脚本) import os import datetime #access.log路径 accesslogs_path = "/usr/local/nginx/logs/" #昨天的日期 lastDate = datetime.date.today() - datetime.timedelta(days=1) #备份路径 bak_path = "%s%s/%s/" % (accesslogs_path,lastDate.strftime(‘%Y‘),lastDate.strftime(‘%m‘)) #nginx日志 access_log = "%saccess.log" % (accesslogs_path) #备份后日志 bak_log = "%s%s.log" % (bak_path,lastDate.strftime(‘%Y%m%d‘)) def bakup(): if os.path.exists(bak_path): print "备份目录已存在。" else: print "备份目录不存在。" print "创建备份目录..." os.makedirs(bak_path) print "开始备份nginx日志..." os.rename(access_log,bak_log) os.system(‘kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`‘) if __name__ == ‘__main__‘: bakup()
时间: 2024-11-10 12:59:04