Created
July 3, 2015 09:30
-
-
Save tawateer/d72a2dde49ee11ffca7b to your computer and use it in GitHub Desktop.
Nginx 日志切割脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# set the path to nginx log files | |
log_files_path="/home/work/nginx/logs/" | |
#log_files_dir=${log_files_path}/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m") | |
log_files_dir="$log_files_path" | |
# set nginx log files you want to cut | |
log_files_type=(access error) | |
# set the path to nginx_pid file. | |
nginx_pid_file="/var/run/nginx.pid" | |
# Set how long you want to save | |
save_days=3 | |
############################################ | |
#Please do not modify the following script # | |
############################################ | |
[ ! -d "$log_files_dir" ] && mkdir -p $log_files_dir | |
log_files_num=${#log_files_type[@]} | |
# cut nginx log files | |
log_files_date=$(date -d "yesterday" +"%Y%m%d") | |
for((i=0;i<$log_files_num;i++)) | |
do | |
for j in $(ls ${log_files_path}/*${log_files_type[i]}.log) | |
do | |
log_files_name=$(basename $j) | |
#echo ${log_files_name} | |
mv ${log_files_path}/${log_files_name} ${log_files_dir}/${log_files_name}-${log_files_date} | |
done | |
done | |
kill -USR1 $(cat ${nginx_pid_file}) | |
# delete $save_days days ago nginx log files | |
[ ! -z $log_files_path ] && find $log_files_path -type f -mtime +$save_days -exec rm -f {} \; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment