Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of using logrotate to manage log files in Linux system

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "the method of using logrotate to manage log files in Linux system". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the method of using logrotate to manage log files in Linux system".

On Debian or Ubuntu:

The code is as follows:

# apt-get install logrotate cron

On Fedora,CentOS or RHEL:

The code is as follows:

# yum install logrotate crontabs

The configuration file for logrotate is / etc/logrotate.conf, which usually does not need to be modified. The round robin of the log file is set in a separate configuration file, which is placed in the / etc/logrotate.d/ directory.

Example one

In the first example, we will create a log file / var/log/log-file for 10MB. We will show how to use logrotate to manage the log file.

Let's start by creating a log file and fill it with 10MB's random bitstream data.

The code is as follows:

# touch / var/log/log-file

# head-c 10m

< /dev/urandom >

/ var/log/log-file

Now that the log file is ready, we will configure logrotate to take turns on the log file. Let's create a configuration file for this file.

The code is as follows:

# vim / etc/logrotate.d/log-file

/ var/log/log-file {

Monthly

Rotate 5

Compress

Delaycompress

Missingok

Notifempty

Create 644 root root

Postrotate

/ usr/bin/killall-HUP rsyslogd

Endscript

}

Here:

Monthly: log files are rotated on a monthly basis. Other available values are 'daily','weekly'' or 'yearly'.

Rotate 5: five archive logs will be stored at a time. For the sixth archive, the oldest archive will be deleted.

Compress: after the round robin task is completed, the rotated archives will be compressed using gzip.

Delaycompress: always used with the compress option, which instructs logrotate not to compress the most recent archive, which will occur in the next round robin cycle. This is useful when you or any software still needs to read the latest archives.

Missingok: during log rotation, any errors, such as "file cannot be found", will be ignored.

Notifempty: if the log file is empty, the round robin will not occur.

Create 644 root root: creates a new log file with the specified permissions, and logrotate renames the original log file.

Postrotate/endscript: after all other instructions are completed, the commands specified in postrotate and endscript will be executed. In this case, the rsyslogd process will immediately read its configuration again and continue to run.

The above template is generic, while the configuration parameters are adjusted according to your needs, not all parameters are necessary.

Example two

In this case, we only want to take a log file, but the log file size can grow to 50MB.

The code is as follows:

# vim / etc/logrotate.d/log-file

/ var/log/log-file {

Size=50M

Rotate 5

Create 644 root root

Postrotate

/ usr/bin/killall-HUP rsyslogd

Endscript

}

Sample three

We want the old log file to be named after the creation date, which can be achieved by adding dateext Changshu.

The code is as follows:

# vim / etc/logrotate.d/log-file

/ var/log/log-file {

Monthly

Rotate 5

Dateext

Create 644 root root

Postrotate

/ usr/bin/killall-HUP rsyslogd

Endscript

}

This will allow archived files to contain date information in their file names.

Removing obstacles

Here are some troubleshooting tips for logrotate settings.

1. Run logrotate manually

Logrotate can be invoked manually from the command line at any time.

To invoke logrotate for all logs configured under / etc/lograte.d/:

The code is as follows:

# logrotate / etc/logrotate.conf

To call logrotate for a specific configuration:

The code is as follows:

# logrotate / etc/logrotate.d/log-file

two。 Exercise

The best choice during troubleshooting is to use the'- d 'option to run logrotate in rehearsal mode. To verify, without actually taking any log files, you can simulate the walkthrough log round robin and display its output.

The code is as follows:

# logrotate-d / etc/logrotate.d/log-file

As we can see from the output above, logrotate determines that this round-robin is unnecessary. This will happen if the file time is less than one day.

3. Forced rotation

Even if the round-robin condition is not met, we can force logrotate to round-robin the log file by using the'- f 'option, where the'-v 'parameter provides detailed output.

The code is as follows:

# logrotate-vf / etc/logrotate.d/log-file

Reading config file / etc/logrotate.d/log-file

Reading config info for / var/log/log-file

Handling 1 logs

Rotating pattern: / var/log/log-file forced from command line (5 rotations)

Empty log files are rotated, old logs are removed

Considering log/ var/log/log-file

Log needs rotating

Rotating log/ var/log/log-file, log- > rotateCount is 5

Dateext suffix'- 20140916'

Glob pattern'- [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9]'

Renaming / var/log/log-file.5.gz to / var/log/log-file.6.gz (rotatecount 5, logstart 1, I 5)

Old log/ var/log/log-file.5.gz does not exist

Renaming / var/log/log-file.4.gz to / var/log/log-file.5.gz (rotatecount 5, logstart 1, I 4)

Old log/ var/log/log-file.4.gz does not exist

. . .

Renaming / var/log/log-file.0.gz to / var/log/log-file.1.gz (rotatecount 5, logstart 1, I 0)

Old log/ var/log/log-file.0.gz does not exist

Log/ var/log/log-file.6.gz doesn't exist-won't try to dispose of it

Renaming / var/log/log-file to / var/log/log-file.1

Creating new / var/log/log-file mode = 0644 uid = 0 gid = 0

Running postrotate script

Compressing log with: / bin/gzip

4. Log of Logrotate

Logrotate's own logs are usually stored in the / var/lib/logrotate/status directory. If we want logrotate to log to any specified file for troubleshooting purposes, we can specify it from the command line as follows.

The code is as follows:

# logrotate- vf-s / var/log/logrotate-status / etc/logrotate.d/log-file

5. Logrotate scheduled tasks

The cron tasks required by logrotate should be automatically created during installation. I will post the contents of the cron file for your reference.

The code is as follows:

# cat / etc/cron.daily/logrotate

#! / bin/sh

# Clean non existent log file entries from status file

Cd / var/lib/logrotate

Test-e status | | touch status

Head-1 status > status.clean

Sed's / "/ / g' status | while read logfile date

Do

[- e "$logfile"] & & echo "\" $logfile\ "$date"

Done > > status.clean

Mv status.clean status

Test-x / usr/sbin/logrotate | | exit 0

/ usr/sbin/logrotate / etc/logrotate.conf

In summary, the logrotate tool is useful to prevent running out of storage space due to large log files. After configuration, the process is fully automatic and can be run for a long time without human intervention. This tutorial focuses on a few basic examples of using logrotate, which you can also customize to suit your needs.

At this point, I believe you have a deeper understanding of "the method of using logrotate to manage log files in the Linux system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report