This Bash script is designed to clean up old log files, systemd journals, Docker resources, and temporary/session files on a Linux system. It can be configured via a .env file and supports automatic self-updates from a remote repository.
Find a file
Michael Kleger 8c67b03010 Version 1.0.6
- Change Update / Install URL
2026-06-18 15:31:43 +02:00
cleanup.sh Version 1.0.6 2026-06-18 15:31:43 +02:00
README.md Version 1.0.6 2026-06-18 15:31:43 +02:00

Cleanup Script

Description

This Bash script is designed to clean up old log files, systemd journals, Docker resources, and temporary/session files on a Linux system. It can be configured via a .env file and supports automatic self-updates from a remote repository.


Features

  • Deletes old log files based on configurable retention time and patterns
  • Cleans systemd journal logs
  • Removes unused Docker images, containers, and volumes
  • Cleans up session/temp files while retaining a specified number
  • Supports configuration via .env file
  • Automatically checks for script updates (can be disabled)

Installation

Install the script using curl:

curl https://git.onesystems.ch/System-Tools/Cleanup/raw/branch/main/cleanup.sh | bash -s install

This will:

  • Download the script to /opt/cleanup/cleanup.sh
  • Set execute permissions
  • Create a symlink in /etc/cron.daily to run daily

Configuration

After the first run, a .env file will be created in the same directory. You can modify it to configure the script:

Example .env:

MONATE_ALT=3
LOG_PFADE=("/var/log" "/opt/ddd/log")
LOG_MUSTER=("*.gz" "*.log.[0-9]*" "*.log.[0-9]*.gz")

JOURNAL_BEREINIGEN=true
JOURNAL_MAX_ALT="3months"

DOCKER_BEREINIGEN=true

SESSION_PFADE=("/var/www/app/sessions" "/opt/tempfiles")
SESSION_DATEIEN_BEHALTEN=100
SESSION_MIN_ALT_TAGE=1

ONLINE_UPDATE=true

Variable Descriptions:

  • MONATE_ALT: Log files older than this (in months) will be deleted
  • LOG_PFADE: Paths to log directories
  • LOG_MUSTER: File name patterns to match for deletion
  • JOURNAL_BEREINIGEN: Whether to clean up systemd journal logs
  • JOURNAL_MAX_ALT: Retention policy for journal logs (e.g. "3months")
  • DOCKER_BEREINIGEN: Whether to clean up Docker resources
  • SESSION_PFADE: Paths to session/temp directories
  • SESSION_DATEIEN_BEHALTEN: Minimum number of session files to retain
  • SESSION_MIN_ALT_TAGE: Minimum age (in days) for session files to be deleted
  • ONLINE_UPDATE: Enables or disables automatic update checks

Logging

  • All actions are logged in /opt/cleanup/logs/cleanup-YYYYMMDD-HHMMSS.log
  • Logs older than 7 days are automatically removed

Manual Execution

You can run the script manually at any time:

/opt/cleanup/cleanup.sh

Notes

  • The script checks for updates from the GitLab repository and updates itself if a new version is available.
  • You can disable this behavior by setting ONLINE_UPDATE=false in the .env file.

License

MIT License free for commercial and private use.