DEV Community

Cover image for 23.Linux LogRotate
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

23.Linux LogRotate

Lab Information

The Nautilus DevOps team is ready to launch a new application, which they will deploy on app servers in Stratos Datacenter. They are expecting significant traffic/usage of tomcat on app servers after that. This will generate massive logs, creating huge log files. To utilise the storage efficiently, they need to compress the log files and need to rotate old logs. Check the requirements shared below:

a. In all app servers install tomcat package.

b. Using logrotate configure tomcat logs rotation to monthly and keep only 3 rotated logs.

(If by default log rotation is set, then please update configuration as needed)

Lab Solutions

🧭 Part 1: Lab Step-by-Step Guidelines

1️⃣ Login to each app server

ssh tony@stapp01
# Password: Ir0nM@n

ssh steve@stapp02
# Password: Am3ric@

ssh banner@stapp03
# Password: BigGr33n

Then:

sudo -i

Enter fullscreen mode Exit fullscreen mode

2️⃣ Install Tomcat and logrotate on ALL app servers

yum install -y tomcat
yum install -y logrotate
Enter fullscreen mode Exit fullscreen mode

3️⃣ Verify Tomcat logs location

ls /var/log/tomcat
Enter fullscreen mode Exit fullscreen mode

👉 Typical logs:

catalina.out
localhost.log

4️⃣ Configure logrotate for Tomcat

Create config file:

vi /etc/logrotate.d/tomcat
Enter fullscreen mode Exit fullscreen mode

5️⃣ Add this configuration

/var/log/tomcat/*.log {
    monthly
    rotate 3
    compress
    missingok
    notifempty
    copytruncate
}
Enter fullscreen mode Exit fullscreen mode

6️⃣ Save file

ESC
:wq

7️⃣ Test logrotate config (important)

logrotate -d /etc/logrotate.d/tomcat
Enter fullscreen mode Exit fullscreen mode

👉 Output

WARNING: logrotate in debug mode does nothing except printing debug messages!  Consider using verbose mode (-v) instead if this is not what you want.

reading config file /etc/logrotate.d/tomcat
Reading state from file: /var/lib/logrotate/logrotate.status
error: error opening state file /var/lib/logrotate/logrotate.status: No such file or directory
Allocating hash table for state file, size 64 entries

Handling 1 logs

rotating pattern: /var/log/tomcat/*.log  monthly (3 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/tomcat/*.log
  log /var/log/tomcat/*.log does not exist -- skipping
Creating new state
Enter fullscreen mode Exit fullscreen mode

8️⃣ Repeat on all app servers

Do same steps on:

stapp01
stapp02
stapp03


🧠 Part 2: Simple Step-by-Step Explanation (Beginner Friendly)

What this lab is about

You are managing:

Large log files → need to control size and storage

Problem

Tomcat generates logs continuously:

More traffic → bigger log files → storage issues
Solution: logrotate
logrotate = tool to manage log files automatically

What your config does

1️⃣ monthly

Rotate logs once every month

2️⃣ rotate 3

Keep only 3 old log files

Example:

catalina.log
catalina.log.1
catalina.log.2
catalina.log.3
(old ones deleted)

3️⃣ compress

Old logs are compressed → save space

4️⃣ copytruncate

Copy log → clear original → service keeps writing

👉 Important because:

Tomcat is still running

5️⃣ missingok

Ignore if log file is missing

6️⃣ notifempty

Skip rotation if log is empty

Final flow

Logs grow → monthly rotation → old logs compressed → only 3 kept

⚡ Key Concept

Log rotation = prevent disk full issues

🎯 Key Takeaway

Production systems ALWAYS rotate logs to save storage


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
📖 More Deep Dives: Whispering Cloud Insights - Read other technical articles
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)