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

0

版权声明 ▶ 本网站名称:我的学习笔记
▶ 本文链接:https://ooolo.net/article/532.html
▶ 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行核实删除。
▶ 转载本站文章需要遵守:商业转载请联系站长,非商业转载请注明出处!!!

none
最后修改于:2024年07月10日 09:44

添加新评论

icon_mrgreen.pngicon_neutral.pngicon_twisted.pngicon_arrow.pngicon_eek.pngicon_smile.pngicon_confused.pngicon_cool.pngicon_evil.pngicon_biggrin.pngicon_idea.pngicon_redface.pngicon_razz.pngicon_rolleyes.pngicon_wink.pngicon_cry.pngicon_surprised.pngicon_lol.pngicon_mad.pngicon_sad.pngicon_exclaim.pngicon_question.png