#!/bin/bash
for((i=1;i<=5;));
do
cpu_us=`top -bn 1 | grep 'Cpu(s)' | awk -F'[" "%]+' '{print $3}'`
cpu_sy=`top -bn 1 | grep 'Cpu(s)' | awk -F'[" "%]+' '{print $5}'`
# Mem total
mem_total=$(free|sed -n '2p'|awk -F ' ' '{print $2}')
# Mem free
mem_free=$(free|sed -n '2p'|awk -F ' ' '{print $4}')
# Mem used
mem_used=$(free|sed -n '2p'|awk -F ' ' '{print $3}')
#echo $cpu_us
#echo $cpu_sy
#echo $mem_total
#echo $mem_free
#echo $mem_used
cpu_sum=$(echo "$cpu_us+$cpu_sy"|bc)
# Mem used_rate
mem_used_rate=$(echo "$mem_used $mem_total" |awk '{printf("%.1f",($1/$2)*100)}')
nowdate=$(date +"%Y-%m-%d %H:%M:%S")
echo "$nowdate CPU使用率: $cpu_sum% 内存使用率: $mem_used_rate%" >> ./cpu.log
sleep 5
done