• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Sunday, May 11, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

Using Logrotate To Manage Log Files

How VPS by How VPS
January 1, 2020
in Linux
0
0
SHARES
96
VIEWS
Share on FacebookShare on Twitter

Contents

  1. Introduction
  2. Installation
  3. Understanding Logrotate
    1. More on Configuration
  4. Managing Logs
  5. Conclusion
  6. Want to contribute?


Introduction

Logrotate is a Linux utility that simplifies the administration of log files. It typically runs once a day via a cron job, and manages logs based on customized rules/settings from its config file.

Some of its useful settings include automatic rotation, compression, removal, and mailing of log files.

Installation

Most Linux systems come with Logrotate installed by default. Check if you have it installed on your Vultr instance by issuing the logrotate command. You will see an output with the version of Logrotate that your server is running.

If you don’t have it installed, perform the steps below to proceed with the installation.

On Debian/Ubuntu systems:

sudo apt-get update
sudo apt-get install logrotate

On Redhat/CentOS systems:

sudo yum update
sudo yum install logrotate

Understanding Logrotate

The moving parts that make logrotate run are:

  • The actual tool logrotate.

  • Logrotate’s configuration file located at /etc/logrotate.conf. This file holds the configuration for all log files that Logrotate manages.

  • A daily cron job /etc/cron.daily/logrotate that issues the logrotate command to run based on settings in its configuration file. If this cron job does not exist on your system, create it and add code snippet below to it.

    #!/bin/sh
    /usr/sbin/logrotate /etc/logrotate.conf
    

More on Configuration

If you take a peek inside /etc/logrotate.conf, you will see that it has the line include /etc/logrotate.d in it. What this line does is tell Logrotate to look inside the /etc/logrotate.d directory and run every configuration file in it. This directory is typically where applications installed on your linux system will add their logrotate configurations. For example, Apache2 will typically create a /etc/logrotate.d/apache configuration file upon installation.

Managing Logs

To test Logrotate, we will:

  • Create a new test log file with 1MB or random data:

    sudo base64 /dev/urandom | head -c 1000000 > /tmp/testfile.log
    
  • Create a new Logrotate configuration by running sudo nano /etc/logrotate.d/testlog. Copy the following snippet into empty file and hit Ctrl + X to save and exit.

    /tmp/testfile.log {
        size 1k
        copytruncate
        missingok
        rotate 5
    }
    

The configuration options in the snippet above instruct Logrotate to:

  • size 1k: Rotate log file if size is greater than or equal to 1k.
  • missingok: Ignore error messages if testfile.log does not exist.
  • copytruncate: Create a copy of current log file and then truncate it. This comes in handy when an application cannot close its log file because it continuously appends to it.
  • rotate 5: limit the number of log file rotations to 5. This will delete old versions of log files greater than 5 days.

NOTE: You can see all configuration options for logrotate here.

  • Run the Logrotate command manually:

    sudo logrotate /etc/logrotate.conf
    

    After running above command, list all files in tmp directory ls -l /tmp to confirm testfile.log was indeed rotated. You should see a listing similar to the following that shows testfile.log has indeed been rotated. This will continue every day and keep the latest 5 copies.

    [root@vultr ~]# ls -l /tmp
    -rw-r--r--  1 root root       0 Nov 14 23:31 testfile.log
    -rw-r--r--  1 root root 1000000 Nov 14 23:30 testfile.log-20141114
    -rw-r--r--  1 root root     634 Nov 10 00:23 vultr_ipv6
    -rw-------. 1 root root       0 Oct 15 20:44 yum.log
    

Conclusion

Logs can quickly create problems on a server by becoming too large and causing disk space issues. Managing logs is crucial for any system, but even better is automating this management. Logrotate makes rotating, archiving, and deleting logs easy.

Written by Lami Adabonyan

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article
Previous Post

How to Install Gitea on Fedora 29

Next Post

Install MediaWiki on the One-Click LEMP Application

Next Post

Install MediaWiki on the One-Click LEMP Application

Leave a Reply Cancel reply

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

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet