Clean logs when no AmlVmService
If you install the frontend (amlvmws4, dof) on a server without the AmlVmService, logs generated are never deleted, use the following to create simple crontab task to clean old logs.
Below are two example Bash scripts for Ubuntu that will help you:
- Create an hourly cron job to delete files older than X hours in a specified folder.
- Remove that same cron job later.
1. Create the Cleanup Task
Script: create_cleanup_task.sh
#!/bin/bash
#
# create_cleanup_task.sh <FOLDER_PATH> <HOURS>
#
# This script creates:
# 1) A cleanup script in /usr/local/bin/ that removes files older than <HOURS> in <FOLDER_PATH>.
# 2) An hourly cron job entry to run that cleanup script.
# ---[ 1. Parse inputs ]---
FOLDER_PATH="$1"
HOURS="$2"
# If either argument is missing, print usage and exit
if [ -z "$FOLDER_PATH" ] || [ -z "$HOURS" ]; then
echo "Usage: $0 <FOLDER_PATH> <HOURS>"
echo "Example: $0 /var/log 6"
exit 1
fi
# ---[ 2. Create the cleanup script ]---
SCRIPT_NAME="cleanup_${FOLDER_PATH//\//_}.sh"
SCRIPT_PATH="/usr/local/bin/${SCRIPT_NAME}"
cat <<EOF > "$SCRIPT_PATH"
#!/bin/bash
# Delete all files older than $HOURS hours from $FOLDER_PATH
find "$FOLDER_PATH" -type f -mmin +\$(( $HOURS * 60 )) -exec rm -f {} \;
EOF
# Make it executable
chmod +x "$SCRIPT_PATH"
# ---[ 3. Create (or update) the hourly cron job ]---
# This sets the job to run at minute 0 of every hour (0 * * * *).
# Adjust if you prefer a different schedule.
( crontab -l 2>/dev/null | grep -v "$SCRIPT_PATH" ; echo "0 * * * * $SCRIPT_PATH" ) | crontab -
echo "=== Cleanup script created at: $SCRIPT_PATH"
echo "=== Cron job added: runs hourly to remove files older than $HOURS hour(s) in $FOLDER_PATH"
How It Works
- Arguments: You provide the directory to clean and the number of hours.
- Cleanup Script: It creates a small script (
cleanup_FOLDERNAME.sh) under/usr/local/bin/. - Cron Job: It adds an entry in your user’s crontab that runs every hour on the hour, calling the cleanup script.
Example Usage
# Make the script executable
chmod +x create_cleanup_task.sh
# Create an hourly cleanup task for /var/log older than 6 hours
./create_cleanup_task.sh /var/log 6
2. Remove the Cleanup Task
Script: remove_cleanup_task.sh
#!/bin/bash
#
# remove_cleanup_task.sh <FOLDER_PATH>
#
# This script:
# 1) Removes the associated cron job.
# 2) Deletes the cleanup script in /usr/local/bin/.
# ---[ 1. Parse input ]---
FOLDER_PATH="$1"
if [ -z "$FOLDER_PATH" ]; then
echo "Usage: $0 <FOLDER_PATH>"
echo "Example: $0 /var/log"
exit 1
fi
SCRIPT_NAME="cleanup_${FOLDER_PATH//\//_}.sh"
SCRIPT_PATH="/usr/local/bin/${SCRIPT_NAME}"
# ---[ 2. Remove the cron job ]---
crontab -l 2>/dev/null | grep -v "$SCRIPT_PATH" | crontab -
# ---[ 3. Remove the cleanup script ]---
if [ -f "$SCRIPT_PATH" ]; then
rm -f "$SCRIPT_PATH"
fi
echo "=== Removed cleanup cron job and script for folder: $FOLDER_PATH"
How It Works
- Argument: You specify the same folder path that was used when creating the task.
- Cleanup: The script uses
grep -vto filter out any line containing the cleanup script path from your current crontab, and overwrites the crontab. - Removal: It deletes the corresponding cleanup script from
/usr/local/bin/.
Example Usage
# Make the script executable
chmod +x remove_cleanup_task.sh
# Remove the cleanup task for /var/log
./remove_cleanup_task.sh /var/log
Notes / Tips
- Cron Permissions: If you want to run the job as
root, you can either: - Switch to root via
sudo su -and then runcrontab -e(or the scripts). - Or explicitly install cron jobs in
root’s crontab usingsudo crontab -e. - Safety Checks: Adjust your
findcommand with a-printtest run first if you want to see which files will be removed:find "$FOLDER_PATH" -type f -mmin +\$(( $HOURS * 60 )) -print - Different Schedules: If hourly is too frequent or not frequent enough, edit the
cronline (e.g.,0 */4 * * *for every 4 hours, etc.). - Time Calculation:
-mmin +Nuses minutes. So for X hours, it’s(X * 60)minutes.
With these two scripts, you can easily schedule automated file cleanups and remove them whenever you like.