• Home
  • Archive
  • Tools
  • Contact Us

The Customize Windows

Technology Journal

  • Cloud Computing
  • Computer
  • Digital Photography
  • Windows 7
  • Archive
  • Cloud Computing
  • Virtualization
  • Computer and Internet
  • Digital Photography
  • Android
  • Sysadmin
  • Electronics
  • Big Data
  • Virtualization
  • Downloads
  • Web Development
  • Apple
  • Android
Advertisement
You are here:Home » How to Manage Logfiles with Logrotate

By Abhishek Ghosh November 12, 2017 5:20 am Updated on November 12, 2017

How to Manage Logfiles with Logrotate

Advertisement

In many guides on this website, we talked about log rotation. Logrotate is Linux utility for automatic rotation, compression, mailing log files etc jobs. Here is how to manage logfiles with logrotate. Many common programs automatically setup Logrotate. Like when we install Apache2, that adds a configuration file used by Logrotate to rotate all apache access and error logs/etc/logrotate.d/apache2. In our other guides, we only mentioned about logrotate script when we built the software from source and/or was sure that the configuration file is not supplied. Logrotate is available on all Linux distributions but default configuration may vary. We are talking in context of Ubuntu 16.04 server. For the others, readers may need checking manual for minimal need of changes. First, we need to check whether logrotate is installed by running :

Vim
1
logrotate --version

If not installed, then we can run the below command(s) to install :

Vim
1
2
3
4
5
6
# Debian/Ubuntu
sudo apt update
sudo apt install logrotate
# Redhat/CentOS
sudo yum update
sudo yum install logrotate

Of course, there is manual of logrorate :

Advertisement

---

Vim
1
man logrotate

 

How to Manage Logfiles with Logrotate

 

/etc/logrotate.conf file contains default settings and includes statement to pull in configuration files from /etc/logrotate.d directory. /etc/logrotate.d/ directory contains the configuration files saved by various applications and utilities. Nothing existing files inside /etc/logrotate.d/ should be altered without knowing about that particular software.

How to Manage Logfiles with Logrotate

For the most daemon processes, logs would be rotated by the root user. logrotate usually invoked from a script in the /etc/cron.daily/ directory.

We can run logrotate as a cronjob to ensure that logs will be rotated as configured. If we configure logrotate to rotate logs every day, but logrotate actually runs every week, the logs will only be rotated every week.

If nothing exist inside /etc/cron.daily/ directory, we need to create a script named /etc/cron.daily/logrotate to do the job :

/etc/cron.daily/logrotate
Vim
1
2
#!/bin/sh
logrotate /etc/logrotate.conf

If I run cat on a server where Apache2 installed :

Vim
1
cat /etc/logrotate.d/apache2

I will get :

Vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/var/log/apache2/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
                if invoke-rc.d apache2 status > /dev/null 2>&1; then
                    invoke-rc.d apache2 reload > /dev/null 2>&1;
                fi;
endscript
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then
run-parts /etc/logrotate.d/httpd-prerotate;
fi;
endscript
}

You need demystification of those phrases.

  • weekly: Rotate logs once per week
  • monthly: Rotate once a month
  • daily: Rotate once a day
  • missingok: Missing OK. If no log files are found, don’t debug or print error.
  • rotate 14: Keep 14 files before deleting old log files
  • compress: Compress (gzip) log files
  • delaycompress: Delays compression until 2nd time rotating.
  • compresscmd: Set which command to used to compress. Defaults to gzip.
  • uncompresscmd: Set the command to use to uncompress. Defaults to gunzip.
  • notifempty: Don’t rotate empty files
  • create 640 root adm: Create new log files with set permissions/owner/group, here user is root and group is adm, permission is 640
  • sharedscripts: Run postrotate script after all logs are rotated
  • postrotate: Scripts to run after rotating is done.
  • size=10M : command sets the minimum size for the rotation to take place to 10M.
  • dateext: This option appends a date instead (logs by default get a number appended to their filename)
  • dateformat: The format of the date appended to the log filename you want.
  • prerotate: Run before log rotating begins

You can run this to test :

Vim
1
2
3
4
# dry run
logrotate -d /etc/logrotate.d/apache2
#
# sudo logrotate /etc/logrotate.d/apache2 --debug

Check what ran by running :

Vim
1
cat /var/lib/logrotate/status

I got a big list like this :

Vim
1
2
3
4
5
6
7
8
9
logrotate state -- version 2
"/var/log/syslog" 2017-11-11-6:25:1
"/var/log/dpkg.log" 2017-11-1-6:25:3
"/var/log/unattended-upgrades/unattended-upgrades.log" 2017-11-1-6:25:3
"/var/log/unattended-upgrades/unattended-upgrades-shutdown.log" 2017-5-26-6:0:0
"/var/log/auth.log" 2017-11-5-6:25:2
"/var/log/apt/term.log" 2017-11-1-6:25:3
...
...

Facebook Twitter Pinterest

Abhishek Ghosh

About Abhishek Ghosh

Abhishek Ghosh is a Businessman, Surgeon, Author and Blogger. You can keep touch with him on Twitter - @AbhishekCTRL.

Here’s what we’ve got for you which might like :

Articles Related to How to Manage Logfiles with Logrotate

  • Join/Merge Multiple Log Files For Big Data Analysis

    Here Are The Ways To Join/Merge Multiple Log Files For Big Data Analysis, Store Them To OpenStack Based Cloud Storage And Delete Old Files.

  • Nginx WordPress Installation Guide (All Steps)

    This is a Full Nginx WordPress Installation Guide With All the Steps, Including Some Optimization and Setup Which is Compatible With WordPress DOT ORG Example Settings For Nginx.

  • unix Commands to Check Server Logs For Security

    Here is a Basic Guide For the Beginners to Self Manage Cloud Server Instances. Master these unix Commands to Check Server Logs For Security.

  • How To Install Apache2 on Ubuntu 18.04 With Let’s Encrypt, HTTP/2, HSTS

    Here is Detailed Guide on How To Install Apache2 on Ubuntu 18.04 With Let’s Encrypt, HTTP/2, HSTS With Commands and Configurations For Most Secured Setup.

performing a search on this website can help you. Also, we have YouTube Videos.

Take The Conversation Further ...

We'd love to know your thoughts on this article.
Meet the Author over on Twitter to join the conversation right now!

If you want to Advertise on our Article or want a Sponsored Article, you are invited to Contact us.

Contact Us

Subscribe To Our Free Newsletter

Get new posts by email:

Please Confirm the Subscription When Approval Email Will Arrive in Your Email Inbox as Second Step.

Search this website…

 

Popular Articles

Our Homepage is best place to find popular articles!

Here Are Some Good to Read Articles :

  • Cloud Computing Service Models
  • What is Cloud Computing?
  • Cloud Computing and Social Networks in Mobile Space
  • ARM Processor Architecture
  • What Camera Mode to Choose
  • Indispensable MySQL queries for custom fields in WordPress
  • Windows 7 Speech Recognition Scripting Related Tutorials

Social Networks

  • Pinterest (24.3K Followers)
  • Twitter (5.8k Followers)
  • Facebook (5.7k Followers)
  • LinkedIn (3.7k Followers)
  • YouTube (1.3k Followers)
  • GitHub (Repository)
  • GitHub (Gists)
Looking to publish sponsored article on our website?

Contact us

Recent Posts

  • Hybrid Multi-Cloud Environments Are Becoming UbiquitousJuly 12, 2023
  • Data Protection on the InternetJuly 12, 2023
  • Basics of BJT TransistorJuly 11, 2023
  • What is Confidential Computing?July 11, 2023
  • How a MOSFET WorksJuly 10, 2023
PC users can consult Corrine Chorney for Security.

Want to know more about us?

Read Notability and Mentions & Our Setup.

Copyright © 2023 - The Customize Windows | dESIGNed by The Customize Windows

Copyright  · Privacy Policy  · Advertising Policy  · Terms of Service  · Refund Policy