What if your Jenkins server suddenly stops because of full disk space? Learn how log rotation saves the day!
Why Log management and rotation in Jenkins? - Purpose & Use Cases
Imagine you are running a Jenkins server that builds many projects daily. Each build creates logs that grow bigger and bigger. Without managing these logs, your server's disk fills up quickly, causing Jenkins to slow down or even crash.
Manually checking and deleting old logs is slow and easy to forget. It's like trying to clean a messy room by picking up one item at a time without a plan. This leads to errors, lost important logs, or running out of space unexpectedly.
Log management and rotation automatically organize logs by size or date. Old logs are compressed or deleted safely, keeping the system clean and fast. This is like having a smart cleaning robot that tidies your room regularly without you lifting a finger.
rm -rf /var/log/jenkins/*
logrotate /etc/logrotate.d/jenkins
It enables Jenkins to run smoothly without interruptions, while keeping important logs accessible and disk space free.
A Jenkins admin sets up log rotation to keep only the last 7 days of build logs compressed. This prevents the server from running out of space and avoids downtime during critical deployments.
Manual log cleanup is slow and risky.
Log rotation automates safe log management.
This keeps Jenkins stable and disk space healthy.
Practice
Solution
Step 1: Understand what logs do in Jenkins
Logs record Jenkins activity and can grow large over time.Step 2: Purpose of log rotation
Log rotation moves or archives old logs to save disk space and keep the system tidy.Final Answer:
To move old logs and save disk space -> Option AQuick Check:
Log rotation = save space [OK]
- Thinking log rotation deletes Jenkins jobs
- Confusing log rotation with backup
- Believing log rotation speeds up builds
copytruncate in a Linux logrotate config for Jenkins logs?Solution
Step 1: Identify correct logrotate syntax
Thecopytruncateoption is valid and used to copy then truncate the log file without stopping Jenkins.Step 2: Check rotation frequency and count
Usingdailyandrotate 7is correct syntax for daily rotation keeping 7 files.Final Answer:
Correct syntax with copytruncate, daily rotation, and rotate 7 -> Option CQuick Check:
copytruncate + daily + rotate 7 = correct [OK]
- Using 'truncatecopy' instead of 'copytruncate'
- Writing 'rotate seven' instead of 'rotate 7'
- Using invalid rotation frequency like 'monthly' with wrong number
/var/log/jenkins/jenkins.log {
copytruncate
weekly
rotate 4
}
What will happen after 5 weeks of running this rotation?Solution
Step 1: Understand rotate count meaning
Rotate 4 means keep 4 old log files; older ones get deleted.Step 2: Apply to 5 weeks
After 5 weeks, the oldest log (week 1) is deleted, keeping only weeks 2-5.Final Answer:
Only the latest 4 weekly logs are kept; older logs are deleted -> Option DQuick Check:
rotate 4 means keep 4 logs [OK]
- Thinking all logs are kept forever
- Assuming logs delete immediately without backups
- Believing Jenkins stops during copytruncate rotation
copytruncate, but logs keep growing without rotation. What is the most likely cause?Solution
Step 1: Check if logrotate runs
If logrotate is not running or scheduled, logs won't rotate despite correct config.Step 2: Consider other causes
Misspelling or wrong path cause errors, but usually logs won't grow if Jenkins stopped.Final Answer:
The logrotate service is not running or not scheduled -> Option BQuick Check:
Logrotate must run to rotate logs [OK]
- Assuming Jenkins must be stopped for rotation
- Ignoring logrotate service status
- Overlooking typos in config file
Solution
Step 1: Identify safe rotation without stopping Jenkins
copytruncatecopies and truncates logs safely without stopping Jenkins.Step 2: Check rotation frequency and retention
Daily rotation withrotate 10keeps 10 days of logs.missingokandnotifemptyhandle missing or empty logs safely.Final Answer:
Config with copytruncate, daily rotation, rotate 10, missingok, notifempty -> Option AQuick Check:
copytruncate + daily + rotate 10 = safe rotation [OK]
- Using monthly or weekly instead of daily rotation
- Not using copytruncate causing Jenkins stop
- Setting rotate less than 10 days
Ready to prove it?
Pass the test and this pattern moves unknown
