Nginx Log Cutting script sharing _linux shell

Source: Internet
Author: User

Implementation method One

#!/bin/bash
logs_path= "/data/application/nginx/logs"
pid_path= "/data/application/nginx/nginx.pid"
Month= ' Date +%y-%m '
date= ' date +%y-%m-%d '
time= ' date +%h ' waittime=$ (
(24*60*60))

logcut ()
{
  CD $Logs _path
  mkdir-p $Month while
  true
  do
    mv $Logs _path/access.log $Logs _path/$Month/ Access_$date.log
    kill-usr1 ' cat ${pid_path} '
  $WaitTime return
  0
}

In
start)
    logcut >/dev/null &;
Stop)
    kill-9 ' ps aux | grep logcut | grep-v grep | awk ' {print $} ' >/dev/null
    ;;
Restart)
    kill-9 ' ps aux | grep logcut | grep-v grep | awk ' {print $} ' >/dev/null Logcut
    ;
    ;;
*)
    echo "Usage error! Please use "Start" or "Stop" or "restart"!
Esac

Implementation Method Two

Test environment:

# cat/etc/redhat-release 
Red Hat Enterprise Linux Server Release 5.3 (Tikanga)

#/opt/nginx/nginx-v
nginx ve rsion:nginx/1.6.2

Code:

#!/bin/bash # ============================================================================== # chmod u+x/opt/nginx/  cut_nginx_log.sh # crontab-e # 0 0 * * */opt/nginx/cut_nginx_log.sh >/opt/nginx/logs/cut_nginx_log.log 2>&1 #
============================================================================== LOGS_PATH= "/opt/nginx/logs" archive_year=$ (date-d "Yesterday" "+%y") archive_month=$ (date-d "Yesterday" "+%m") archive_date=$ (date-d "Yesterday" " +%y%m%d_%h%m%s ") if [-r/opt/nginx/nginx.pid]; Then Mkdir-p "${logs_path}/${archive_year}/${archive_month}" mv "${logs_path}/access.log" "${logs_path}/${archive_ Year}/${archive_month}/access_${archive_date}.log "KILL-USR1 $ (cat"/opt/nginx/nginx.pid ") Sleep 1 gzip" ${LOGS_PAT H}/${archive_year}/${archive_month}/access_${archive_date}.log "Else echo" Nginx might is down "fi # ================== ============================================================ # Clean up log files older than # ============================================================================== # change Housekeeping=1 to enable clean up Housekeeping=0 keep_days=100 If [$HOUSEKEEPING = 1]; Then if [-D "${logs_path}"];
  Then find "${logs_path}"-type f-name "access_*.log.gz"-mtime +${keep_days}-exec rm-f {} \; Fi fi

Reference:
Http://wiki.nginx.org/LogRotation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.