脚本一:
cat cut_nginx_log.sh
#!/bin/bash
#该脚本用于nginx日志分割,方便日志分析
#access.log日志所在目录
ACCESS_LOGS_PATH=/usr/local/nginx/logs
#error.log日志所在目录
ERROR_LOGS_PATH=/usr/local/nginx/logs
#切割日志目录
BCK_DIR=/usr/local/nginx/logs/log_backup
mkdir -p $BCK_DIR
#日志更名
mv ${ACCESS_LOGS_PATH}/access.log ${BCK_DIR}/access.log_$(date +%Y%m%d)
mv ${ERROR_LOGS_PATH}/error.log ${BCK_DIR}/error.log_$(date +%Y%m%d)
#生成新的日志文件
kill -USR1 $(cat /var/run/nginx/nginx.pid)
find $BCK_DIR -mtime +30 -name "*.log_*" -exec rm -rf {} \;
crontab -l #计划任务
0 0 * * * sh /root/shell/cut_nginx_log.sh >/dev/null 2>&1
脚本二:
cat cut_nginx_log.sh
#!/bin/bash
Log_dir="/usr/local/nginx/logs"
Log_bak_dir="/usr/local/nginx/logs/bak"
Day=$(date -d "1 day ago" +"%Y%m%d")
Pid_path="/var/run/nginx/nginx.pid"
cd $Log_dir
for i in $(ls *.log);do
{
mv $i ${Log_bak_dir}/$i.$Day
} &
done
wait
kill -USR1 $(cat $Pid_path)
find ${Log_bak_dir} -mtime +30 -name "*.log.*" -exec rm -rf {} \;