In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
I. introduction to the Logistic tool
Logrotate is a log file management tool, it is a log cutting tool that comes with Linux by default. Used to rotate, compress, delete old files, and create new log files. We can dump the log files according to the size and number of days of the log files, so as to facilitate the management of log files, which is usually done through cron planning tasks, so that log cutting can be divided by hour, day, and so on.
II. Operating mechanism of Logistic
The system runs logrotate regularly, usually once a day. This is how the system executes on a daily basis. Crontab executes the scripts in the / etc/cron.daily directory regularly every day, and there is a file called logrotate in this directory.
[root@test01 ~] # cat / ETC logrotate ALERT exited abnormally with [$EXITVALUE] "fiexit 0] then / usr/bin/logger-t logrotate
When actually running, Logrotate invokes the configuration file / etc/logrotate.conf
III. Composition of Logistic
/ usr/sbin/logrotate # where the program is located
/ etc/cron.daily/logrotate # lets Cron execute logrotate once a day by default
/ etc/logrotate.conf # Global profile
/ etc/logrotate.d/ # applies its own configuration file storage directory to overwrite the global configuration
Note: logrotate.d is a directory in which all files are actively read into / etc/logrotate.conf. In addition, if no details are set in the file in / etc/logrotate.d/, the settings in the file / etc/logrotate.conf will be used as the default value.
IV. The command format of Logistic
Logrotate [OPTION...]
-d,-- debug: debug mode to test the configuration file for errors.
-f,-- force: forcibly dump files.
-m,-- mail=command: after compressing the log, send the log to the specified mailbox.
-s,-- state=statefile: uses the specified status file.
-v,-- verbose: shows the dump process.
For example: want to forcibly cut log files, do not wait for logrotate cutting.
[root@test01] # / usr/sbin/logrotate-vf / etc/logrotate.d/tomcat # I used to test hey hey note: the CentOS7 command is in / usr/sbin/.
five。 Description of common configuration parameters
The most important thing for the realization of Logrotate function is its configuration parameters.
Here are some of its common configuration parameters:
Compress
Enable compression, which refers to the old logs after rotation. Gzip compression is used by default here.
Daily
Rotate every day
Dateext
Use the current date as the naming format
Dateformat. S
When used with dateext, it appears immediately after the next line. The file name after the cut of the definition file must be used with dateext. Only the four parameters of% Y% m% d% s are supported.
Ifempty
Rotate even if the log file is empty
Send the alternate file to the specified E-mail address
Copytruncate
For log files that are still open, backing up and truncating the current log is a way to copy first and then empty it. There is a time difference between copy and emptying, and some log data may be lost.
Monthly
Rotate once a month
Nocompress
If compression is enabled in logrotate.conf, here are the parameters that do not need compression.
Nomail
Do not send mail to any address
Notifempty
If the log is empty, it will not be rotated (that is, the empty log will not be cut)
Olddir + directory
After rotation, the log files are placed in the specified directory and must be on the same file system as the current log files.
Number of rotate +
Rotate to retain the previous data at most several times, the excess will be deleted or received, and set to 0, it will not be saved.
Size size
When the log grows to the specified size, it starts to rotate.
Weekly
If the current day of the week is less than the day of the last rotation, or more than a week has passed, the rotation will occur, usually on the first day of the week. If the logrotate is not run every day, it will be rotated when the first opportunity is available.
Yearly
If the current year is different from the last rotation year, log rotation occurs.
Missingok
If the log is lost, do not report an error and continue to scroll to the next log
six。 Install and configure Logrotate
# install under yum
[root@test01 ~] # yum-y install logrotate
# tomcat has been installed on the machine. Configure the configuration file for cutting tomcat logs below
[root@test01 ~] # vim / etc/logrotate.d/tomcat
Note: the configuration file for logrotate is / etc/logrotate.conf, which usually does not need to be modified.
The cutting cycle of log files is set in a separate configuration file, and they are placed in the / etc/logrotate.d/ directory.
[root@test01 ~] # mkdir-p / var/log/tomcat/oldlog [root@test01 ~] # cat/ etc/logrotate.d/tomcat/usr/local/tomcat8/logs/catalina.out {# the log path to be cut is separated by spaces if it is multiple. If the log is empty, there is no rotation (that is, empty log is not sliced) rotate 5000 # rotation retains the previous data up to 5000 times missingok # if the log is lost Do not report an error continue to scroll the next log compress # enable compression, which refers to the old log after rotation. By default, dateext # compressed by gzip uses the current date as the naming format dateformat.% Y%m%d-%H dot # is used with dateext, which appears immediately after the next line. The file name after the cut of the definition file must be used with dateext. Only% Y% m% d% s olddir / var/log/tomcat/oldlog # after rotation log files are supported in the specified directory}
Note: my configuration does not set how often to cut, but it is also cut once a day, because when the / etc/logrotate.d/tomcat file is not configured, the default is to execute the / etc/cron.daily/logrotate file at a regular time every day, this file is once a day.
# enforce cutting to see if there is a log under / var/log/tomcat/oldlog
[root@test01] # / usr/sbin/logrotate-vf / etc/logrotate.d/tomcatreading config file / etc/logrotate.d/tomcatolddir is now / var/log/tomcat/oldlogAllocating hash table for state file, size 15360 BHandling 1 logsrotating pattern: / usr/local/tomcat8/logs/catalina.out forced from command line (5000 rotations) olddir is / var/log/tomcat/oldlog, empty log files are not rotated, old logs are removedconsidering log/ usr/local/tomcat8/logs/catalina.out log needs rotatingrotating log/ usr/local/tomcat8/logs/catalina.out Log- > rotateCount is 5000Converted'.% Y%m%d-%H point'- >.% Y%m%d-%H point 'dateext suffix'. 20181226-15:00 'glob pattern'. [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]-[0-9] [0-9] point 'glob finding old rotated logs failedfscreate context set to unconfined_u:object_r:usr_ T:s0renaming / usr/local/tomcat8/logs/catalina.out to / var/log/tomcat/oldlog/catalina.out.20181226-15:00 compressing log with: / bin/gzipset default create context to unconfined_u:object_r:usr_t:s0set default create context
# View the cutting result
# delete the previous cut, because the rescheduled file is the same, or the catalina.out.20181226-15:00 .gz file, so the cut will not be successful.
[root@test01 ~] # rm-rf / var/log/tomcat/oldlog/catalina.out.20181226-15:00. GZ [root@test01] # ls / var/log/tomcat/oldlog/ [root@test01 ~] #
# set scheduled tasks and cut them every minute (note: there should be content in the log, because the empty log files defined earlier are not cut)
[root@test01 ~] # crontab-vf etc/logrotate.d/tomcat 1 * / usr/sbin/logrotate-vf / etc/logrotate.d/tomcat > > / var/log/tomcat/oldlog/cutting.log
# cut every two hours
[root@test02 ~] # crontab-10 * / 2 * / usr/sbin/logrotate-vf / etc/logrotate.d/tomcat > > / root/chenjiaxin/cutting.log
# View the results of timing cutting
Of course, the above is set to cut once a minute, so that the experiment can see the effect as soon as possible. In fact, it is necessary to set how often to cut the log according to the needs of the company!
The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.