logrotate Command in Linux

Learn via video courses
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

The logrotate command in Linux is an essential tool designed for managing log files produced by system processes. When a system's log files become too large, they can consume significant disk space, leading to potential system instability. The logrotate command provides an efficient solution to this, enabling automatic rotation, compression, and removal of log files. This prevents disk space from being overwhelmed, ensuring smooth operation of your Linux system.

Syntax of logrotate Command in Linux

The basic syntax of the logrotate command in Linux is as follows:

Where:

  • options: These are flags or parameters that modify the behavior of the logrotate command. They are optional and vary depending on your specific use case.
  • config_file: This is the configuration file that logrotate uses to determine how to manage specific log files. It contains the set of rules that logrotate follows.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Options in logrotate Command in Linux

  1. -d, --debug: This option allows for a dry run, showing what would happen without actually executing the command.

    For example -

    Output:

    This command simulates log rotation without actually executing it, showing what would happen based on the rules in the specified configuration file.

  2. -f, --force: This option forces the logrotate command to rotate logs, even if it doesn't think it's necessary.

    For example -

    This command forces log rotation according to the rules specified in the provided configuration file, regardless of whether logrotate thinks rotation is needed.

  3. -v, --verbose: This option makes the operation more talkative, providing detailed output about what the logrotate command is doing.

    For example -

    Output:

    This command rotates the logs as per the rules defined in the configuration file and provides detailed output about the operations being performed.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example Usages

  • Basic usage of logrotate command with a configuration file.:

    Explanation: This command will rotate the logs based on the instructions specified in the '/etc/logrotate.conf' configuration file.

  • Forcing log rotation even if logrotate thinks it's not necessary.:

    Explanation: This command forces log rotation according to the rules specified in the provided configuration file, regardless of whether logrotate thinks rotation is needed.

Tips

  • Log files can grow large quickly. Regularly checking them and using the logrotate command can help keep them manageable.

  • Always test new logrotate configuration files with the --debug option before deploying them in a live environment.

  • For complex log management tasks, consider creating custom configuration files for different sets of log files.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Advanced Use Cases of logrotate Command in Linux

  • Creating a custom logrotate configuration for a specific application.:

    Explanation: This command creates a new logrotate configuration file for 'myapp'. It will rotate its logs daily, keep a week's worth of logs, and compress the rotated logs.

  • Forcing log rotation on all configuration files in the logrotate directory.:

    Explanation: This command will force the rotation of all logs that have a configuration file in the '/etc/logrotate.d/' directory.

  • Checking the status of logrotate operations.:

    Output:

    Explanation: This command will display the status of logrotate, including the last time the logs were rotated for each file. In this case, the '/var/log/syslog' file was last rotated on 2023-5-16 at 6:25:1.

Conclusion

  • The logrotate command in Linux is a powerful tool for managing system log files, helping to prevent disk space issues.

  • It offers numerous options for customizing how and when logs are rotated, compressed, and removed.

  • Using the --debug option to test new configuration files is recommended before deploying them.

  • For complex log management, creating custom configuration files for different sets of log files can provide more precise control.