NO IMAGE

nginx訪問日誌的切割,每十分鐘切割一次,放到定時任務中

*/10 * * * * sh nginx_log_cut.sh
#!/bin/sh
# Function:For cut nginx'log every 10 min
# script_name: nginx_log_cut.sh
. /etc/profile
function move_log()
{
local log=$1
local date=$2
if [ -f $log ]
then
if [ -s $log ]
then
/bin/mv $log /data/logs/backup/nginx/`echo $log|awk -F'nginx/' '{print $2}'`.$date
fi
fi
}
NGINX_BIN="/usr/local/nginx/sbin/nginx"
REOPEN="$NGINX_BIN -s reopen"
NGINX_CONF=`ls /usr/local/nginx/conf/conf.d/ | grep .conf$`
for nginx_conf in $NGINX_CONF
do
LOGS="`/bin/cat /usr/local/nginx/conf/conf.d/$nginx_conf | /bin/grep -e "access_log\s*\/data\/log" | /bin/grep -v '^\s*#' |/bin/sed -e 's/.$//' -e 's/^\s*access_log\
s*//g' | /usr/bin/awk '{print $1}'| /bin/sed 's/;//g' | /bin/sort -u`"
echo $LOGS
DATE=`/bin/date ' %Y%m%d%H%M%S'`
for log_file in `/bin/echo $LOGS`
do
move_log $log_file $DATE
done
$REOPEN >/dev/null 2>&1
done
find /data/logs/backup/nginx  -mtime  30 -type f |xargs rm -f