A robust, systemd-driven automation tool designed to manage disk space by purging old log archives on a monthly schedule. This implementation uses a systemd timer to ensure reliability even across system reboots.
cleanup.service: Defines the execution logic and the script to be run.cleanup.timer: Defines the schedule and persistence logic./usr/local/bin/cleanup-tool: The executable tool that performs the deletion.
Follow these steps to clone the project and install the cleanup tool on your local system.
git clone https://github.com/2Kelvin/archives-cleanup.git
# follow that project's readme for setup and installationThe service is configured as a oneshot type, meaning it executes the command and exits immediately without leaving a background process resident in memory.
[Unit]
Description=Delete old log archives
[Service]
Type=oneshot
ExecStart=/usr/local/bin/cleanup-tool /var/logThe timer is set to trigger monthly. With Persistent=true, if the system is powered off during the scheduled time, systemd will trigger the service immediately upon the next boot.
[Unit]
Description=Run clean up of logs once every month
[Timer]
OnCalendar=monthly
Persistent=true
Unit=cleanup.service
[Install]
WantedBy=timers.targetFollow these steps to install and activate the automation:
-
Copy Files: Place both the
.serviceand.timerfiles into/etc/systemd/system/:sudo cp {cleanup.timer,cleanup.service} /etc/systemd/system/ -
Reload Daemon Inform systemd of the new configuration files:
systemctl daemon-reload
-
Enable and Start Enable the timer so it persists across reboots and trigger it immediately:
systemctl enable --now cleanup.timer
If you modify the timer or service configuration, apply the changes using:
[!NOTE] You do not need to run the enable command again after edits unless you have renamed the files.
To verify the status of your timer and check when it is scheduled to run next, use the following commands:
| Command | Purpose |
|---|---|
systemctl list-timers --all |
View all active timers and their next scheduled run. |
systemctl status cleanup.timer |
Check if the timer is active and "waiting." |
journalctl -u cleanup.service |
View the execution logs/output of the cleanup script. |
If you ever want to test if cleanup.service logic actually works without waiting an entire month for the timer to trigger, you can manually trigger the service part alone like this:
systemctl start cleanup.serviceThis runs the script immediately regardless of the timer schedule.
- Stop and delete the .timer first
sudo systemctl stop cleanup.timer
# then
sudo systemctl disable cleanup.timer- Stop and delete the .service
sudo systemctl stop cleanup.service
# then
sudo systemctl disable cleanup.service- Delete actual .service and .timer files in
/etc/systemd/system/
cd /etc/systemd/system/
# then
sudo rm {cleanup.timer, cleanup.service}- Finally, reload systemctl with new changes
sudo systemctl daemon-reload