search
HomeSystem TutorialLINUXHow to use logrotate to manage Linux log files

How to use logrotate to manage Linux log files

Feb 10, 2024 am 11:00 AM
linuxlinux tutoriallinux systemlinux commandshell scriptembeddedlinuxGetting started with linuxlinux learning

Log files in Linux systems are important resources for recording system operation and troubleshooting. However, as time goes by, log files will continue to grow, occupying a large amount of disk space, and are not conducive to log viewing and analysis. In order to solve this problem, we can use the logrotate tool to automatically rotate, compress, delete, and send emails to log files to achieve effective management of log files. This article will introduce the basic principles, configuration files and common options of the logrotate tool, as well as how to configure logrotate strategies for different applications.

How to use logrotate to manage Linux log files

How logrotate works

By default, the logrotate command is run once a day as a cron task placed in /etc/cron.daily. It will help you set a policy in which log files exceeding a certain time or size are rotated.
Order:

/usr/sbin/logrotate

Configuration file: /etc/logrotate.conf, this is the main configuration file of logrotate. logrotate also stores service-specific configuration in /etc/logrotate.d/. Make sure the line below is included in /etc/logrotate.conf to read service-specific log configuration.

include  /etc/logrotate.d`

logrotate history: /var/lib/logrotate.status

Important logrotate options:

compress             --> 压缩日志文件的所有非当前版本
daily,weekly,monthly --> 按指定计划轮换日志文件
delaycompress        --> 压缩所有版本,除了当前和下一个最近的
endscript            --> 标记 prerotate 或 postrotate 脚本的结束
errors "emailid"     --> 给指定邮箱发送错误通知
missingok            --> 如果日志文件丢失,不要显示错误
notifempty           --> 如果日志文件为空,则不轮换日志文件
olddir "dir"         --> 指定日志文件的旧版本放在 “dir” 中
postrotate           --> 引入一个在日志被轮换后执行的脚本
prerotate            --> 引入一个在日志被轮换前执行的脚本
rotate 'n'           --> 在轮换方案中包含日志的 n 个版本
sharedscripts        --> 对于整个日志组只运行一次脚本
size='logsize'       --> 在日志大小大于 logsize(例如 100K,4M)时轮换

Configuration

Let's configure logrotate for our own sample log file /tmp/sample_output.log.

Step one: Add the following lines to /etc/logrotate.conf.

/tmp/sample_output.log {
  size 1k
  create 700 root root
  rotate 4
  compress
}

In the above configuration file:

size 1k - logrotate 仅在文件大小等于(或大于)此大小时运行。
create - 轮换原始文件并创建具有指定权限、用户和组的新文件。
rotate - 限制日志文件轮转的数量。因此,这将只保留最近的 4 个轮转的日志文件。
compress - 这将压缩文件。

Step 2: Usually, you need to wait a day for logrotate to be executed by /etc/cron.daily. Otherwise, you can run it from the command line with:

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

Output before executing logrotate command:

[root@rhel1 tmp]# ls -l /tmp/
total 28
-rw-------. 1 root root 20000 Jan 1 05:23 sample_output.log

Output after executing logrotate:

[root@rhel1 tmp]# ls -l /tmp
total 12
-rwx------. 1 root root 0 Jan 1 05:24 sample_output.log
-rw-------. 1 root root 599 Jan 1 05:24 sample_output.log-20170101.gz
[root@rhel1 tmp]#

This will confirm that logrotate is successfully implemented.

Through the introduction of this article, we understand the function and usage of the logrotate tool, and how to customize the logrotate strategy according to different needs. The logrotate tool can help us save disk space, improve the readability and usability of log files, and also facilitate us to back up and monitor log files. The logrotate tool is a very practical log management tool in Linux systems and is worth mastering and using.

The above is the detailed content of How to use logrotate to manage Linux log files. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
Zenity: Building GTK  Dialogs in Shell ScriptsZenity: Building GTK Dialogs in Shell ScriptsMay 13, 2025 am 09:38 AM

Zenity is a tool that allows you to create graphical dialog boxes in Linux using the command line. It uses GTK , a toolkit for creating graphical user interfaces (GUIs), making it easy to add visual elements to your scripts. Zenity can be extremely u

Top 22 Best Music Players for LinuxTop 22 Best Music Players for LinuxMay 13, 2025 am 09:25 AM

Some may describe it as their passion, while others may consider it a stress reliever or a part of their daily life. In every form, listening to music has become an inseparable part of our lives. Music plays different roles in our lives. Sometimes it

How to Create Fillable PDF Forms on Linux with ONLYOFFICEHow to Create Fillable PDF Forms on Linux with ONLYOFFICEMay 13, 2025 am 09:15 AM

PDF (Portable Document Format) was invented many years ago by Adobe. It is currently the most popular format for sharing information due to its ease of use, security, reliability, and compatibility with all devices we use on a daily basis. This forma

How to Restrict SSH Access to Local Networks on LinuxHow to Restrict SSH Access to Local Networks on LinuxMay 13, 2025 am 09:07 AM

SSH (Secure Shell) is a popular tool that allows users to connect to remote systems securely over a network. By default, SSH is accessible from any network as long as the appropriate firewall and network settings are in place. However, sometimes you

How does memory management differ between Linux and Windows?How does memory management differ between Linux and Windows?May 13, 2025 am 12:04 AM

LinuxandWindowsmanagememorydifferentlyduetotheirdesignphilosophies.Linuxusesovercommittingforbetterperformancebutrisksout-of-memoryerrors,whileWindowsemploysdemand-pagingandmemorycompressionforstabilityandefficiency.Thesedifferencesimpactdevelopmenta

How to Manage Firewalld and UFW for Linux SecurityHow to Manage Firewalld and UFW for Linux SecurityMay 12, 2025 am 10:56 AM

Linux systems rely on firewalls to safeguard against unauthorized network access. These software barriers control network traffic, permitting or blocking data packets based on predefined rules. Operating primarily at the network layer, they manage

How to Check If Your Linux System is a Desktop or LaptopHow to Check If Your Linux System is a Desktop or LaptopMay 12, 2025 am 10:48 AM

Determining if your Linux system is a desktop or laptop is crucial for system optimization. This guide outlines simple commands to identify your system type. The hostnamectl Command: This command provides a concise way to check your system's chassis

How to Increase TCP/IP Connections in LinuxHow to Increase TCP/IP Connections in LinuxMay 12, 2025 am 10:23 AM

Guide to adjust the number of TCP/IP connections for Linux servers Linux systems are often used in servers and network applications. Administrators often encounter the problem that the number of TCP/IP connections reaches the upper limit, resulting in user connection errors. This article will guide you how to improve the maximum number of TCP/IP connections in Linux systems. Understanding TCP/IP connection number TCP/IP (Transmission Control Protocol/Internet Protocol) is the basic communication protocol of the Internet. Each TCP connection requires system resources. When there are too many active connections, the system may reject new connections or slow down. By increasing the maximum number of connections allowed, server performance can be improved and more concurrent users can be handled. Check the current number of Linux connections limits Change settings

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft