Detailed record memory usage shell script

Remember a recent memory test. Since the monitoring GUI used cannot be traced back to the previous two days, I plan to write a script to record it every 3 minutes, and the record includes the memory usage of the service, and it will be cleaned up once a week.

Note: The log record content needs to follow its own directory structure

sudo vim /data/changxuan/memwrite.sh
#!/bin/bash

while true; do
  # 时间戳
  timestamp=$(date +"%Y-%m-%d %H:%M:%S")

  # 内存占用统计
  mem=$(free -m | awk 'NR==2{printf "%.2f", $3/1024}')

  # 详细划分内存占用进程
  process=$(ps aux --sort=-%mem | awk 'NR==2{print $11}')

  # 写入文件
  echo "$timestamp $process $mem MB" >> /data/changxuan/mem.txt

  # 3分钟一次
  sleep 180
done

Clean up once a week and add regular content

crontab -e -u market
0 0 * * 0 > /data/changxuan/mem.txt

 

Guess you like

Origin blog.csdn.net/m0_72264240/article/details/130499740