CentOS7服务器分区监控脚本需求:实现带时间戳的df -h输出存储
Hey there! Let's get your disk monitoring script sorted out—you've got two solid options depending on how you want to store the logs. Let's walk through both, plus fix the issues in your current script:
方案1:每次生成带时间戳的独立日志文件
If you want a new .txt file every hour with a unique timestamp (so you can easily track when each check happened), use this script:
#!/bin/bash # Generate a timestamp in YYYYMMDD_HHMMSS format (adjust the format if you prefer) TIMESTAMP=$(date +"%Y%m%d_%H%M%S") # Define where you want to save the logs (replace with your actual path) LOG_DIR="/home/your_username/disk_logs" # Make sure the log directory exists (create it if not) mkdir -p "$LOG_DIR" # Run df -h and save output to the timestamped file df -h > "${LOG_DIR}/disk_usage_${TIMESTAMP}.txt"
关键说明:
- Replace
/home/your_username/disk_logswith a directory you have write access to (create it first if it doesn't exist, or themkdir -pcommand will handle it automatically). - The timestamp format
%Y%m%d_%H%M%Sgenerates names like20240520_143000—you can adjust this using standard date placeholders (e.g.,%Y-%m-%d_%H-%M-%Sfor hyphen-separated values).
方案2:追加到同一个日志文件(带时间戳标记)
If you'd rather keep all logs in one file with clear timestamps for each run (easier to scroll through history), use this version:
#!/bin/bash # Define your single log file path (replace with your preferred location) LOG_FILE="/home/your_username/disk_logs/disk_usage_history.log" # Create the log file if it doesn't exist touch "$LOG_FILE" # Add a timestamp header to separate each run echo "=== Disk Usage Check - $(date '+%Y-%m-%d %H:%M:%S') ===" >> "$LOG_FILE" # Append the df -h output df -h >> "$LOG_FILE" # Add a blank line to make the log easier to read echo "" >> "$LOG_FILE"
关键说明:
- The
>>operator appends content to the file (preserving existing logs), whereas>would overwrite the file each time. - The
touchcommand ensures the log file exists before we try to write to it (no errors if it's already there).
后续步骤:
- Make the script executable: Run this command in your terminal (replace the path with your script's location):
chmod +x /path/to/your/script.sh - Set up the cron job:
- Open your user's crontab editor with
crontab -e - Add this line to run the script every hour at the top of the hour:
0 * * * * /path/to/your/script.sh - Save and exit the editor (if using nano, press
Ctrl+O, press Enter, thenCtrl+X).
- Open your user's crontab editor with
Fixing your original script issues:
Your initial script had a couple of small problems that were preventing it from working correctly:
- The
#!/bin/shshebang needs to be on its own line (not followed directly bydf -h). moreis for viewing files interactively, not redirecting output—use>(overwrite) or>>(append) instead.- To include the date, you need to capture its output with
$(date)(like we did in the examples) or echo it directly to the file, rather than just adding thedatecommand randomly.
内容的提问来源于stack exchange,提问作者Choppies007
相关产品推荐
相关产品推荐

