1、编写shell脚本cut_nginx_log.sh,内容如下
#!/bin/bash #设置日志文件存放目录 LOG_HOME="/home/nginx/logs/" access_log=host.access.log; error_log=error.log; pid=`ps -ef |grep -v grep |grep "nginx: master process "|awk -F" " '{print $2}'` yesterday=`date -d '-1 day' +%Y%m%d` #修改备份文件名称 mv ${LOG_HOME}/${access_log} ${LOG_HOME}/${yesterday}.${access_log} mv ${LOG_HOME}/${error_log} ${LOG_HOME}/${yesterday}.${error_log} #向nginx主进程发信号重新打开日志 kill -USR1 ${pid}
2、服务器创建/usr/local/nginx目录并上传cut_nginx_log.sh
3、验证
执行一次sh /usr/local/nginx/cut_nginx_log.sh看日志是否切分正确
4、设置linux定时任务
crontab -e
0 0 * * * sh /usr/local/nginx/cut_nginx_log.sh
5、查看inux定时任务
crontab -l