You've already forked clear-log-ubuntu
261 lines
5.3 KiB
Markdown
261 lines
5.3 KiB
Markdown
|
|
# Clear Logfiles for Ubuntu 24.04 Server
|
|||
|
|
[](#)
|
|||
|
|
[](#)
|
|||
|
|
[](#)
|
|||
|
|
[](./LICENSE)
|
|||
|
|
|
|||
|
|
Install log cleanup and logfile reporting on Ubuntu 24.04 server.
|
|||
|
|
|
|||
|
|
Not a demo, not a quick hack.
|
|||
|
|
This script is built for production systems and safe unattended execution.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Why this installer exists
|
|||
|
|
|
|||
|
|
Ubuntu systems accumulate rotated logfiles over time.
|
|||
|
|
While `logrotate` handles rotation, old rotated logs are not automatically removed, and custom or installer-generated logs can grow unnoticed.
|
|||
|
|
|
|||
|
|
Disk space is rarely the real problem.
|
|||
|
|
Operator clarity is.
|
|||
|
|
|
|||
|
|
An unmanaged `/var/log` directory becomes noisy over time and increases the risk of misreading timestamps or confusing old incidents with new ones during troubleshooting — especially under pressure.
|
|||
|
|
|
|||
|
|
In environments without centralized log storage, keeping `/var/log` clean ensures it remains a usable, live operational workspace.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## What this script does
|
|||
|
|
|
|||
|
|
✔ Scans `/var/log` recursively
|
|||
|
|
✔ Deletes classic rotated logfiles created by logrotate
|
|||
|
|
✔ Detects and reports large unrotated logfiles
|
|||
|
|
✔ Detects non-logrotate numeric logfiles (installer and debug logs)
|
|||
|
|
✔ Writes a full execution report to a logfile
|
|||
|
|
✔ Designed for unattended cron execution
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## What this script does NOT do
|
|||
|
|
|
|||
|
|
It won’t stop you from running the script without reading the documentation like there’s no tomorrow.
|
|||
|
|
Skip the README, and whatever happens next is your headache, not a bug report.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 1. Download
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
git clone https://git.x-files.dk/system/clear-log-ubuntu.git
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
cd clear-log-ubuntu
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 2. Install
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
sudo cp clear-log-files /usr/local/sbin/
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Recommended first run
|
|||
|
|
|
|||
|
|
The script is published in dry-run mode by default so nothing gets deleted.
|
|||
|
|
|
|||
|
|
Run the script manually the first time.
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
sudo /usr/local/sbin/clear-log-files
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
Review the generated report in:
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
/var/log/clear-log-files.log
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
Once you are satisfied with the behavior, edit the script and set:
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
dryrun=false
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
Only then should the script be deployed via cron.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Cron usage
|
|||
|
|
|
|||
|
|
Once verified, add a weekly cron job. This can quickly be done from the command line like this:
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
sudo tee /etc/cron.d/clear-log-files >/dev/null <<EOF
|
|||
|
|
0 1 * * 5 root /usr/local/sbin/clear-log-files >/dev/null 2>&1
|
|||
|
|
|
|||
|
|
EOF
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
## Configuration
|
|||
|
|
|
|||
|
|
The two most important variables are defined at the top of the script.
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
dryrun=true
|
|||
|
|
extended_cleanup=false
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### dryrun
|
|||
|
|
|
|||
|
|
- `true`
|
|||
|
|
Default. No files are removed. All actions are simulated and reported.
|
|||
|
|
|
|||
|
|
- `false`
|
|||
|
|
Enables real cleanup. Intended for cron usage after verification.
|
|||
|
|
|
|||
|
|
### extended_cleanup
|
|||
|
|
|
|||
|
|
- `false`
|
|||
|
|
Non-logrotate numeric logs are reported only (default and recommended).
|
|||
|
|
|
|||
|
|
- `true`
|
|||
|
|
Non-logrotate numeric logs are deleted during real runs.
|
|||
|
|
|
|||
|
|
Deletion of extended logs requires both:
|
|||
|
|
- `dryrun=false`
|
|||
|
|
- `extended_cleanup=true`
|
|||
|
|
|
|||
|
|
This is intentional.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Script output
|
|||
|
|
|
|||
|
|
Each run writes a full report to:
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
/var/log/clear-log-files.log
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
The report includes:
|
|||
|
|
- which files were removed
|
|||
|
|
- which files were reported only
|
|||
|
|
- warnings about large active logfiles
|
|||
|
|
- execution timestamp and configuration state
|
|||
|
|
|
|||
|
|
This makes audits and troubleshooting straightforward.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Logfile cleanup policy
|
|||
|
|
|
|||
|
|
The script separates logfiles into three distinct classes.
|
|||
|
|
|
|||
|
|
### Classic rotated logs (auto-removed)
|
|||
|
|
|
|||
|
|
Examples:
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
auth.log.1
|
|||
|
|
auth.log.2.gz
|
|||
|
|
kern.log.4.gz
|
|||
|
|
dpkg.log.1
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### Extended rotated logs (reported only by default).
|
|||
|
|
|
|||
|
|
Examples:
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
subiquity-client-debug.log.1946
|
|||
|
|
installer-info.log.1990
|
|||
|
|
customapp.log.999
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
These files are not created by `logrotate`.
|
|||
|
|
|
|||
|
|
By default, they are detected and reported but not deleted. Setting extended_cleanup to true changes this.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### Large unrotated logs (warnings only)
|
|||
|
|
|
|||
|
|
Any active `.log` file exceeding the configured size threshold is reported as a warning no action is taken.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### What this script will "NEVER" delete are:
|
|||
|
|
|
|||
|
|
Kernel logs and audit logs. These are allways kept for forensic needs. This is intentionally by design.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Configuration
|
|||
|
|
|
|||
|
|
The two most important variables are defined at the top of the script.
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
dryrun=true
|
|||
|
|
extended_cleanup=false
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### dryrun
|
|||
|
|
|
|||
|
|
- `true`
|
|||
|
|
Default. No files are removed. All actions are simulated and reported.
|
|||
|
|
|
|||
|
|
- `false`
|
|||
|
|
Enables real cleanup. Intended for cron usage after verification.
|
|||
|
|
|
|||
|
|
### extended_cleanup
|
|||
|
|
|
|||
|
|
- `false`
|
|||
|
|
Non-logrotate numeric logs are reported only (default and recommended).
|
|||
|
|
|
|||
|
|
- `true`
|
|||
|
|
Non-logrotate numeric logs are deleted during real runs.
|
|||
|
|
|
|||
|
|
Deletion of extended logs requires both:
|
|||
|
|
- `dryrun=false`
|
|||
|
|
- `extended_cleanup=true`
|
|||
|
|
|
|||
|
|
This is intentional.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Script output
|
|||
|
|
|
|||
|
|
Each run writes a full report to:
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
/var/log/clear-log-files.log
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
The report includes:
|
|||
|
|
- which files were removed
|
|||
|
|
- which files were reported only
|
|||
|
|
- warnings about large active logfiles
|
|||
|
|
- execution timestamp and configuration state
|
|||
|
|
|
|||
|
|
This makes audits and troubleshooting straightforward.
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### More Information
|
|||
|
|
|
|||
|
|
More guides and documentation can be found on [wiki.x-files.dk](https://wiki.x-files.dk)
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
### License
|
|||
|
|
|
|||
|
|
Licensed under the [MIT License](./LICENSE).
|