linux定时任务执行
#!/bin/bash
# Define the directory where logs are stored
LOG_DIR="/data/scripts"
# Define the maximum file size in bytes (10GB = 10 * 1024 * 1024 * 1024)
MAX_SIZE=$((10*1024*1024*1024))
# Log file for recording execution logs
LOG_FILE="/var/log/cleanup_logs.log"
# Log the current date/time and action
echo "$(date +'%Y-%m-%d %H:%M:%S') - Starting log cleanup" >> "$LOG_FILE"
# Find and delete log files larger than MAX_SIZE bytes recursively
find "$LOG_DIR" -type f -name "*.log" -size +$MAX_SIZEc -print0 | while IFS= read -r -d '' file; do
echo "$(date +'%Y-%m-%d %H:%M:%S') - Deleting $file" >> "$LOG_FILE"
rm -f "$file"
done
# Log the completion
echo "$(date +'%Y-%m-%d %H:%M:%S') - Log cleanup completed" >> "$LOG_FILE"
每30分钟执行一次
*/30 * * * * /home/user/scripts/myscript.sh