shell+ Timing Task +nginx signal Management implementation log cut storage by date

Source: Internet
Author: User

We renamed yesterday's log 00:00:01 every day, placed it in a specific directory, and then USR1 the information number control Nginx to regenerate a new log file

Create a new shell script under directory/usr/local/nginx/logs/runlog.sh
Note: The file location in the following code can be changed by itself

# define the SH interpreter
#!/bin/bush

# defines the storage location of the log to be split
Logpath=/usr/local/nginx/logs/access.log
# defines the storage location of the split log, $ ( Date-d yesterday +%y%m): directory named year + month
basepath=/usr/local/nginx/logs/$ (date-d yesterday +%y%m)

# Recursively creates a basepath-named directory
mkdir-p $BASHPATH
# to String ... Assign value to bak
bak= $BASEPATH/$ (date-d yesterday +%d) _access_log

# will rename $logpath to $bak
mv $LOGPATH $bak
# Create $ LOGPATH file Touch
$LOGPATH

# control nginx rebuild log file
kill-usr1 '/usr/local/nginx/logs/nginx.pid '

Then use the crontab command to edit the timed task

Crontab-e

Enter in the Open edit window

* * * * sh/usr/local/nginx/logs/runlog.sh

The meaning of the crontab file:

# in the crontab file that the user has created, each row represents a task, each field of each row represents a setting, its format is divided into six fields, the first five paragraphs is a time set segment, the sixth paragraph is the command segment to execute, the format is as follows:

# minute hour day month week
Command # Time

of the week and the moon the interpretation of each setting
minute represents the minute, which can be any integer from 0 to 59.
hour represents an hour, which can be any integer from 0 to 23.
Day represents a date, which can be any integer from 1 to 31.
Month represents the month, which can be any integer from 1 to 12. Week represents the day of the
week, which can be any integer from 0 to 7, where 0 or 7 represent Sunday.
command you want to execute, either a system command or a script file that you write yourself.

At this time, that is, set up the nginx of timed log cutting storage, every day 00:00:01 yesterday's log cut storage.

Related Article

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.