You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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_logs with a directory you have write access to (create it first if it doesn't exist, or the mkdir -p command will handle it automatically).
  • The timestamp format %Y%m%d_%H%M%S generates names like 20240520_143000—you can adjust this using standard date placeholders (e.g., %Y-%m-%d_%H-%M-%S for 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 touch command ensures the log file exists before we try to write to it (no errors if it's already there).
后续步骤:
  1. 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
    
  2. 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, then Ctrl+X).
Fixing your original script issues:

Your initial script had a couple of small problems that were preventing it from working correctly:

  • The #!/bin/sh shebang needs to be on its own line (not followed directly by df -h).
  • more is 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 the date command randomly.

内容的提问来源于stack exchange,提问作者Choppies007

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 14:02:50