Bobcares

Logrotate configuration in Linux – How to save disk space?

PDF Header PDF Footer

Logs come useful in finding and correcting errors.

However, large log files use up too much disk space and cause server errors.

Luckily, proper logrotate configuration in Linux servers easily eliminate logs growing big.

At Bobcares, we frequently receive disk full errors as part of our Server Management Services.

Today, we’ll see how our Dedicated Engineers setup logrotate configuration in Linux and save disk space.

 

What is logrotate?

Before proceeding further, let’s have an idea on Logrotate.

Logrotate is a system utility that helps in managing large number of log files. It allows automatic rotation, compression and removal of log files. Additionally, logrotate has the option for mailing of log files too.

In Linux servers, every service has a log file associated with it. It contain entries related to each specific task of the application. Naturally, the log file grows in size with time. As a result, it can consume considerable disk space on the server. Huge size of the log file can also make the service slow.

Moreover, with improper logrotate configuration, the server disk space fills up and eventually service fails. That’s where the benefit of logrotate comes in.

Logrotate is one of the ways to keep old copies of log files on the server. We can set the logrotate utility to compress and keep desired number of old log files.

 

How we setup logrotate configuration?

Logs come handy in troubleshooting application specific errors. But, we cannot keep all the log files on the server due to disk space constraints too.

It’s time now to see how our Dedicated Engineers setup logrotate configuration in linux server to prevent disk full errors. The whole idea is to compress the log file and keep it in rotated status. Basically, there are 2 methods for configuring log rotation on any server. One from the command prompt and another from the control panel.

 

1. From command-line

By default, all servers comes with logrotate utility. And, the configuration file at /etc/logrotate.conf holds a default set of values. Again, the folder /etc/logrotate.d/ contains the logrotation configuration for each service.

Most servers come up with default configuration for common system packages and services like apt, yum, apache, mail, etc.

Again, the configuration further depends on the size of the log file, how quickly the log file builds up and so on. For example, to avoid disk space problems due to large mail server logs, our Support Engineers set up logrotate as

/var/log/maillog {
rotate 4
daily
compress
missingok
notifempty
}

Here, logrotate will rotate the file /var/log/maillog daily and keep just 4 compressed copies of the same. Again, missingok option produce no errors even if the log file is missing. Similarly, the notifempty variable will stop logrotate from taking further action.

After customizing the configuration file as per the needs, we save it in /etc/logrotate.d. Then we do a dry run by executing the command:

logrotate /etc/logrotate.conf --debug

This would print the details on how Logrotate will process the log file.

That’s it. The standard Logrotate job will run once a day as a cron job on the server and include the custom configuration too.

Yet another useful option in Logrorate config is postrotate to endscript. This block contains a script that will be run after rotating the log file. From our expertise in managing servers, most apps need a reload after clearing the log file. In such cases, we add the following in the logrotate configuration.

postrotate
systemctl reload myapp
endscript

Again, logrotate utility work for log rotation of custom files too. Here, we schedule and run the logrotate command as a cron job at desired intervals.

 

2. From control panel

Fortunately, most control panels offer a way to configure logrotate from the admin interface.

For example, Plesk give option to customize the logrotate configuration for each domain. For this, we connect to the panel, then go to Home >> Domains >> Domain.com >> Log Manager.

Here, logrotate command will rotate the file when it reaches a size of 204800 KB. Also it keeps a single compressed copy of old log file.

Similarly, cPanel servers also give options to tweak log rotation settings of cPanel log files. To do this, we connect to the WHM of the server as the root user and tweak the option at Home >> Service Configuration >> cPanel Log Rotation configuration.

 

Common errors in logrotate

While dealing with hosting servers, we often come across  log rotation errors. Let’s take a quick look on how our Dedicated Engineers fix them.

 

1. Incorrect permissions

For logrotate to work properly, the parent folders should have proper permissions. Incorrect permissions or ownership on parent directory can result in logrotate configuration error in linux servers.

For example, a sample error received in newrelic application cron job in Plesk looks as below:

error: skipping "/var/log/newrelic/php_agent.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.

On a detailed investigation, we found that the parent folder of log files, /var, /var/log were not having proper ownership and permissions.

Therefore, we fixed the problem by correcting the permissions and ownership on the parent directories using the command:

chmod 755 /var/log/ && chown root:root /var/log/
chmod 755 /var/log/newrelic && chown root:root /var/log/newrelic
chmod 755 /var && chown root:root /var

 

2. Syntax errors in configuration

Recently one of our customer reported problems with his custom logrotate configuration. It was not processing the logs as per the requirement.

On checking, we found that there were syntax errors in the logrotate configuration and this was making the logrotate cron fail. Therefore, we fixed the syntax of the logrotate configuration file first. Then, we ran the following command to forcefully rotate the log file:

logrotate /home/user/logrotate.conf --state /home/user/logrotate-state --verbose --force

That fixed the problem and rotated the log file. After that, the cron started working correctly.

[Is your logrotate configuration messed up? We can fix it for you.]

 

Conclusion

In short, logrotate configuration in Linux helps to prevent log files becoming bigger. Logrotate rotate and compress log files. Today, we saw how our Dedicated Engineers configure logrotate and save disk space on the server.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";
0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

server management

Spend time on your business, not on your servers.

TALK TO US

Or click here to learn more.

Speed issues driving customers away?
We’ve got your back!

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF