a1e81e2501bbffe41d556d2fb1fb6a46b16befa5
Logrotate Automation Script
This simple Bash script automates the execution of logrotate for specific log files, ensuring that your logs are managed effectively to prevent disk space issues and improve system performance.
Features
- Automated Log Rotation: Automatically runs
logrotatewith a specified configuration. - Status Tracking: Utilizes a status file to keep track of the last rotation times.
- Error Handling: Checks for the existence of the configuration file and logs errors if it's missing.
- Logging: Records the execution status and any errors to a dedicated log file.
Getting Started
These instructions will help you set up and run the script on your system.
Prerequisites
logrotate: This utility is typically pre-installed on most Linux distributions. If not, you can install it using your system's package manager (e.g.,sudo apt-get install logrotateon Debian/Ubuntu,sudo yum install logrotateon CentOS/RHEL).
Installation
- Save the Script: Copy the provided script content into a file (e.g.,
run_logrotate.sh) in your desired directory. - Make Executable: Grant execute permissions to the script:
chmod +x run_logrotate.sh - Create Configuration: Create a
logrotate.cfgfile in the same directory as the script. This file will contain yourlogrotaterules. - Create Log and Status Files: The script will create these if they don't exist, but you can create empty ones manually if you prefer:
touch logrotate.status touch logrotate.log
Usage
Configuration (logrotate.cfg)
The logrotate.cfg file defines how your logs should be rotated. Here's a basic example:
/var/log/my_app/*.log {
daily
rotate 7
compress
missingok
notifempty
create 640 root adm
dateext
}
- Replace
/var/log/my_app/*.logwith the actual path to your log files. daily: Rotate logs daily.rotate 7: Keep 7 rotated log files.compress: Compress old log files.missingok: Don't issue an error if the log file is missing.notifempty: Don't rotate the log if it is empty.create 640 root adm: Create new log files with specified permissions and ownership.dateext: Archive rotated logs by adding a date extension.
For more details on logrotate configuration, refer to the logrotate man page (man logrotate).
Running the Script
You can run the script manually:
./run_logrotate.sh
Automation (Cron Job)
To automate log rotation, it's recommended to set up a cron job.
- Open your crontab for editing:
crontab -e - Add a line to execute the script at your desired interval. For example, to run it daily at 3:00 AM:
Make sure to replace
0 3 * * * /home/oberkoetter/Dokumente/Git/rettedpv/run_logrotate.sh >> /dev/null 2>&1/home/oberkoetter/Dokumente/Git/rettedpv/run_logrotate.shwith the actual full path to your script. The>> /dev/null 2>&1part redirects standard output and error to/dev/nullto prevent unnecessary emails from cron.
Script Details
The script performs the following actions:
- Changes Directory: Navigates to
/home/oberkoetter/Dokumente/Git/rettedpv. Note: You might want to adjust this path if you move the script. - Defines Paths: Sets variables for the
logrotate.cfg,logrotate.status, andlogrotate.logfiles within the current working directory. - Checks Configuration: Verifies if
logrotate.cfgexists. If not, it logs an error and exits. - Executes Logrotate: Runs
logrotatein forced mode (-f) using the specified configuration and status files. - Logs Success: Appends a success message to
logrotate.logafter a successful execution.
Project Structure
.
├── run_logrotate.sh
├── logrotate.cfg
├── logrotate.log
└── logrotate.status
run_logrotate.sh: The main script to execute logrotate.logrotate.cfg: The configuration file for logrotate.logrotate.log: Logs the execution history of the script.logrotate.status: Logrotate's internal status file.
Troubleshooting
- "Fehler: Konfigurationsdatei './logrotate.cfg' nicht gefunden.": Ensure
logrotate.cfgis in the same directory as the script or update theCONFIGvariable in the script to the correct path. - Script not running via cron: Double-check the cron job entry for correct paths and permissions. You can test by manually running the script. Also, check your
logrotate.logfor any errors. - Log files not rotating: Verify your
logrotate.cfgsyntax. You can test yourlogrotateconfiguration directly usinglogrotate -d your_config_file(debug mode) orlogrotate -f your_config_file(force mode).
Description
Languages
Shell
100%