linux定时任务执行 Published on Jul 10, 2024 in 随笔 with 0 comment ``` #!/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 ``` 本文由 admin 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。