1、分割脚本:cut_nginx_logs.sh
#!/bin/bash #cut_nginx_logs.sh #author xingxiudong #set the path to nginx log files log_files_path="/var/log/nginx" #set nginx log files you want to cut project_name=mmt log_files_name=nginx_access_mmt log_files_dir=${log_files_path}/bck/${project_name}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m") set the path to nginx. nginx_sbin="/etc/init.d/nginx" ############################################ #Please do not modify the following script # ############################################ mkdir -p $log_files_dir #log_files_num=${#log_files_name[@]} #cut nginx log files mv ${log_files_path}/${log_files_name}.log ${log_files_dir}/${log_files_name}_$(date -d "yesterday" +"%Y%m%d")_$(date +"%Y%m%d%H%M%S").log kill -USR1 $(cat /var/run/nginx.pid) $nginx_sbin force-reload
2、准备定时执行文件:cron_cur_nginx_logs
1 0 * * * /home/mmt/cut_nginx_logs.sh
sudo chmod 777 /home/mmt/cut_nginx_logs.sh
sudo crontab /home/mmt/cron_cur_nginx_logs
=============================================================
一日,领导要在windows机器上出每日nginx访问日志,无奈写了一个:
taskkill /F /IM nginx.exe set "cmd_zszy=move F:\nginx-access_log\zszy\logs\zszy.log F:\nginx-access_log\zszy\logs\zszy.%date:~0,4%-%date:~5,2%-%date:~8,2%.log call %cmd_zszy%" start nginx
保存后把这个批处理文件放到nginx根目录下,在windows下添加一个任务计划,每天执行一次即可.该批处理一共做了三件事,先杀掉系统中正在运行的nginx进程,然后备份并根据时间重命名日志文件,最后再启动nginx服务。
经过测试,在windows下面必须在nginx服务停止的情况下才能备份日志,这个有点小小的遗憾,不过一般情况下这个备份过程会在1到2秒内完成,把这个批处理放到每天凌晨执行,基本不影响nginx提供服务。