先写切割脚本split_nginx_log.sh:
#!/bin/sh
Dateformat=`date +%Y%m%d%H%M%S`
Basedir="/usr/local/nginx"
NginxLogDir="/usr/local/nginx/logs"
LogName="access"
[ -d $NginxLogDir ] && cd $NginxLogDir||exit 1
[ -f ${LogName}.log ] || exit 1
\mv ${LogName}.log ${LogName}_${Dateformat}.log
$Basedir/nginx -s reload
然后通过Linux下的定时任务crontab命令:
安装crontab:
yum install crontabs
/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入配置
/sbin/service crond status //状态服务
在/var/spool/cron/下建一个root文件,加入:
#split nginx access log
* * * * * sh split_nginx_log.sh > /dev/null 2>&1
重启crond服务:
/sbin/service crond restart
每分钟将nginx的访问日志access.log会切割一次,如下:
access_20170921152501.log