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

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.

systemctl reload myapp

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.]



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.

Source link


Write A Comment