Add a comment. Active Oldest Votes. Improve this answer. Pol Hallen Pol Hallen 2 2 gold badges 10 10 silver badges 18 18 bronze badges. Community Bot 1. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Who owns this outage? Building intelligent escalation chains for modern SRE.
This prevents logs from filling up entire partitions and bringing systems to their knees in the process. You might have noticed log rotation at work on a system nearby. Look at the dates on the boot. In this case, when a specific condition is met, the current file is renamed by appending a date to identify and track it, and a new file with the original name is opened, ready to receive incoming log messages.
As you might expect, logging in Linux systems, such as the CentOS 7 server I am using for illustration, rely on a daemon to facilitate logging. Rsyslogd is the name of the reliable old service and it is open-source. Actually, it is not so old. It is an improved version of the original syslog daemon, and it possesses the ability to quickly process and forward logs to any location in an IP network.
Aside from syslog and rsyslog , there is syslog-ng , which is yet another daemon for handling logs. The default log handler depends on the distro one chooses. Rsyslog comes by default in many Red Hat-based distros. Run the following command to verify its presence and version on your system:. You can choose either option, or even both, to handle your logging needs. For more information, see the documentation for your distribution. This article will assume you are using rsyslogd.
Logrotate is a Linux utility whose core function is to - wait for it - rotate logs. If it is not installed as part of the default OS installation, it can be installed simply by running:. Also, a folder is created for service-specific snap-in configuration files for tailor-made log rotation requests. More on this a bit later. A cron job is run daily that starts the utility.
This is achieved by placing a call to the utility in the standard cron folder for daily jobs. While the specifics of the call are outside the scope of this article, suffice it to say that the call is just a Bash script that runs the logrotate binary, telling the system what to do in case of an error. Logs on a Linux system can be placed anywhere permissions allow.
The first file to take note of with regard to the function of logrotate is logrotate. This config file contains the directives for how log files are to be rotated by default. If there is no specific set of directives, the utility acts according to the directives in this file.
Let's go through a few of the directives to get a feel for the flexibility of logrotate. Graciously, the author of the utility has put in enough comments to get a newbie going. You could also see the man page for a deeper dive. The directives weekly , dateext , compress , create , and rotate 4 state that log files are to be rotated weekly , that the date of rotation be used as the identifying suffix of the rotated files, that the rotated files should be compressed , that a new file is to be created to receive incoming logs, and that no more than four logs should be kept.
In other words, the fifth newest log should be deleted. Also note that on my system, due to the presence of the before the compress directive, compression is disabled by default. Create root root is used create log files immediately after the rotation, as root user and user group root with specific permission mode. Open any editor, add the following settings options and save the file.
Run the following command to create a log file named mylog. Now, open logrotate-state file to examine which is added in the file. Since no log files are generated yet, so the following output will appear. Run the following command to rotate log file forcefully. But no change will appear here according to the previous output because rotation interval is set as monthly and the time period is not exceeded here. Many other logrotate options are available to configure log files.
You will get the list of logrotate options after executing the following command:. Some others mostly used logrotate options are, size, copytruncate, postrotate, maxage and compresscmd. These options are shortly explained here. This option is used to set the limit of log file size and logrotate will rotate log files when the file size reaches or exceed the limit. This option is used to create a copy of the original log files and truncate the original log file size to 0.
So that the service related to that particular log file can write log properly in the original file. This option is used to set time duration for storing old log file information.
It sets the maximum values in days to keep log files and all log files will be removed when the maxage values exceeds. You can compress log files by using different commands. This option is used to specify the type of compression command. After reading this tutorial, you will be able to use Logrotate with different options for managing your log files properly.
Skip to content Home.
0コメント