1. #cut_nginxlog.sh
    2. #!/bin/bash
    3. #by author:albert.dong
    4. #Mailto:dongliang101@gmail.com
    5. # The Nginx logs path
    6. #logs_path="/data/var/logs/tengine"
    7. #logs_name=xxx.com-access.`date -d "+6 hour" +"%F_%H-%M.log"`
    8. #mv ${logs_path}upstream_response_time.log ${logs_path}${upstream_logs_name}
    9. #kill -USR1 `cat /var/run/nginx.pid`
    10. #gzip ${logs_path}${logs_name}
    11. #gzip ${logs_path}${upstream_logs_name}
    12. #0 0 * * * /data/scripts/cut_nginxlog.sh
    13. #######################################################
    14. #set -x
    15. Logs_path="/data/var/logs/tengine/"
    16. NginxPid="/var/run/nginx.pid"
    17. Bakdir="/data/var/logs/tengine/backup"
    18. LogNameList="m.wehobi.com-access nginx_error"
    19. Datedir=`date +%Y%m%d`
    20. #mkdir -p ${Bakdir}/$datedir >/dev/null 2>&1
    21. if [ ! -d "${Bakdir}/${Datedir}" ]; then
    22. mkdir -p ${Bakdir}/${Datedir}
    23. DateBak="${Bakdir}/${Datedir}"
    24. echo $DateBak
    25. fi
    26. if test -d ${Logs_path};then
    27. for name in ${LogNameList}
    28. do
    29. if test -e ${Logs_path}/${name}.log;then
    30. #logs_name=${name}.`date -d "+6 hour" +"%F_%H-%M.log"`
    31. logs_name=${name}.`date +%Y%m%d%H%M.log`
    32. mv ${Logs_path}/${name}.log ${Bakdir}/${Datedir}/${logs_name}
    33. kill -USR1 `cat ${NginxPid}`
    34. gzip ${DateBak}/${logs_name}
    35. #mv ${Logs_path}/${logs_name}.gz ${Bakdir}/${Datedir}
    36. find ${Logs_path} -name "${name}.*.log.gz" -mtime +15 | xargs rm -rf
    37. find ${Logs_path} -type f \( -name "*.gz" -o -name "*.log" \) -mtime +15 -delete
    38. fi
    39. done
    40. fi