In this article, you will learn how to use Logrotate in Linux to manage logs.
Logrotate is a tool in Linux that manages log files by automatically rotating, compressing, and removing old logs. It keeps your system clean and prevents logs from consuming too much disk space.
Why Use Logrotate?
- Prevents large log files from filling up your disk.
- Automates log rotation on a daily, weekly, or monthly basis.
- Compresses old logs to save space.
- Keeps logs manageable for analysis and troubleshooting.
Let’s begin using Logrotate to manage logs in Linux:
- Most Linux distributions come with Logrotate pre-installed. To check or install:
sudo apt install logrotate # Ubuntu/Debian sudo yum install logrotate # CentOS/RHEL
- Logrotate settings are stored at:
- Global config file: /etc/logrotate.conf
- App-specific configs: /etc/logrotate.d/
The global file may include directory-wide settings, while per-application configs go in /etc/logrotate.d/.
- Now, to manage your application logs (e.g., /var/log/myapp.log), create a custom logrotate rule using the below command:
sudo nano /etc/logrotate.d/myapp
And, paste the following configuration:
/var/log/myapp.log { daily rotate 7 compress missingok notifempty copytruncate }Explanation:
daily: Rotate logs every day
rotate 7: Keep the last 7 rotated logs
compress: Compress old logs with gzip
missingok: Skip if file is absent
notifempty: Don’t rotate empty logs
copytruncate: Beneficial if the application can’t close and reopen logs - Use this command to check if log rotation works:
sudo logrotate -d /etc/logrotate.conf # Debug/test sudo logrotate -f /etc/logrotate.conf # Force rotation
This helps confirm if your rules are working properly.
- Logrotate is automatically scheduled to run daily through a cron job located at /etc/cron.daily/logrotate. Manual setup isn’t required unless you need a custom schedule.
Troubleshooting Tips:
- Check “/var/lib/logrotate/status” to see the last rotations.
- Ensure Logrotate has permission to access log files.
- For apps like SigNoz, use “copytruncate” or the app’s log rotation settings.
This way, you can set up Logrotate and easily manage log files in Linux, automating cleanup, saving disk space, and keeping your system organised without manual effort.
For additional assistance, feel free to contact our support staff.
Want to archive logs efficiently? Learn How to compress files into an archive using GZip