Log file rotation script
Igor Clark
igor at pokelondon.com
Mon Jun 9 14:28:38 MSD 2008
i did almost exactly the same for a virtual-hosted server with loads
of logs in e.g. /var/www/$HOSTNAME/logs
#!/bin/sh
#
# nginx_log_rotate.sh
#
# Rotates nginx logs into hour-stamped archive directories
# and gzips with to-the-second timestamps
NGINX_DIR=/path/to/nginx-install
PID_FILE=$NGINX_DIR/logs/nginx.pid
for s in /path/to/web/root/*
do
echo $s
if [ -L $s ] ; then # ignore symlinks, if any
continue
fi
log_dir=$s/logs
date_dir=`date +%Y/%m/%d/%H` # change this to the precision
you want, and match with crontab frequency
if [ -r ${log_dir}/access_log ] && [ -r ${log_dir}/
error_log ] ; then
# make new timed dir
/bin/mkdir -p ${log_dir}/${date_dir} > /dev/null 2>&1
# move in current logs
/bin/mv ${log_dir}/access_log ${log_dir}/${date_dir}/
access_log
/bin/mv ${log_dir}/error_log ${log_dir}/${date_dir}/
error_log
# tell nginx to reopen logs
kill -USR1 `cat $PID_FILE`
# compress old
compress_stamp=`date +%Y%m%d-%H%M%S`
/bin/gzip -S _$compress_stamp.gz ${log_dir}/$
{date_dir}/access_log &
/bin/gzip -S _$compress_stamp.gz ${log_dir}/$
{date_dir}/error_log &
fi
done
On 9 Jun 2008, at 03:07, Dave Cheney wrote:
> I wrote this a while ago which does the trick
>
> #!/bin/bash
>
> YEAR=`date "+%Y"`
> MONTH=`date "+%m"`
> DAY=`date "+%d"`
>
> HOSTNAME=`hostname -s`
>
> LOG_FILES="access.log error.log"
>
> DATE=$YEAR/$MONTH/$DAY
>
> LOG_ROOT=/var/log
> NGINX_LOG_ROOT=$LOG_ROOT/nginx
>
> # make path
>
> mkdir -p $NGINX_LOG_ROOT/$DATE
>
> # touch and symlink in new log files
>
> for FILE in $LOG_FILES; do
> LOG_FILE=$NGINX_LOG_ROOT/$DATE/$HOSTNAME.$FILE
> touch $LOG_FILE
> ln -fs $LOG_FILE $NGINX_LOG_ROOT/$FILE
> done
>
> # tell nginx to re-open its log files
>
> kill -USR1 `cat /var/run/nginx.pid`
>
>
>
>
> On 09/06/2008, at 11:50 AM, Rt Ibmer wrote:
>
>> Hi - does anyone have a log file rotation script for nginx they can
>> share? Basically just looking for something simple I can set up in
>> cron once a day that will tell nginx to roll the log file and set
>> its file name to the current date, and then gzip it. Thanks!
>>
>>
>>
>>
>>
>>
>
>
More information about the nginx
mailing list