Why Your Log Delete Script Failed and How to Fix It Automated log cleanup scripts are vital for server health. When they fail, disks fill up, services crash, and production environments halt. If your cleanup script recently failed, you are not alone.
Here is why your log delete script failed and exactly how to fix it. 1. The “Argument List Too Long” Error The Problem
You used a wildcard command like rm /var/log/app/.log. Over time, your application generated tens of thousands of log files. When the script ran, the shell expanded the wildcard into a massive list of individual filenames. This exceeded the system’s command-line argument limit (ARG_MAX), causing the command to fail entirely.
Use the find command with -delete or xargs. This processes files in manageable batches instead of loading them into memory all at once.
# Correct approach find /var/log/app/ -name “.log” -type f -delete Use code with caution. 2. Deleted Files Still Consuming Disk Space The Problem
Your script successfully ran rm app.log, and the file vanished from the directory listing. However, df -h shows that your disk is still 100% full. This happens because a running application (like Nginx, Apache, or a Java app) still holds an open file descriptor to that log. The operating system will not free the disk space until the application closes the file or restarts.
Never delete an active log file with rm. Instead, truncate the file to zero bytes without breaking the application’s file descriptor.
# Truncate a specific file truncate -s 0 /var/log/app/active.log # Or use shell redirection > /var/log/app/active.log Use code with caution. 3. Unhandled Spaces in Filenames The Problem
Your script loops through files using a standard for loop, like for file in $(ls.log). If a developer or system process creates a log file with a space in the name (e.g., app error.log), the shell treats “app” and “error.log” as two separate files. Your script then attempts to delete non-existent files and skips the real one.
Avoid parsing ls in scripts. Use find with the -print0 option paired with xargs -0, or wrap your loop variables in double quotes.
# Safe deletion for files with spaces find /var/log/app/ -name “.log” -type f -print0 | xargs -0 rm -f Use code with caution. 4. Permission Denied and Absolute Path Failures The Problem
Scripts that run perfectly when manually executed by a user often fail when scheduled via Cron or systemd. This happens for two reasons:
Missing Privileges: The automation runner lacks root or sudo permissions to modify /var/log.
Relative Paths: The script relies on relative paths (like rm ./logs/.log) and executes from the system root directory instead of the intended folder, deleting nothing or wiping out the wrong directory.
Always enforce absolute paths and ensure your automation runner has the correct user permissions.
#!/bin/bash # Enforce absolute paths explicitly TARGET_DIR=“/var/log/app” if [ -d “\(TARGET_DIR" ]; then find "\)TARGET_DIR” -name “.log” -mtime +7 -type f -delete fi Use code with caution. The Ultimate Fix: Switch to Logrotate
Writing custom bash scripts to manage logs is prone to edge-case failures. The industry standard solution is Logrotate, a built-in Linux utility designed specifically to handle rotation, compression, truncation, and deletion safely.
Instead of a custom script, create a configuration file in /etc/logrotate.d/app:
/var/log/app/*.log { daily rotate 7 compress delaycompress missingok notifempty copytruncate } Use code with caution. Why this configuration works: daily: Rotates the logs every day.
rotate 7: Keeps exactly 7 days of history and deletes the oldest automatically. compress: Gzips old logs to save up to 90% of disk space.
copytruncate: Safely frees disk space without crashing running applications.
To help find the exact cause of your script failure, let me know: What operating system and scripting language are you using? What error message did the script output?
Is the script running manually or via an automated scheduler like Cron?
I can provide a customized, copy-pasteable script or config to resolve your issue.
Leave a Reply