基于free -m命令实现
[root@logstash ~]# free -m
total used free shared buff/cache available
Mem: 3931 351 3346 11 234 3355
Swap: 3967 0 3967
[root@logstash ~]#
server_source_memory.sh
#!/bin/bash
function memory()
{
total=$(free -m | awk '{if(NR==2) printf "%.1f",$2/1024}')
used=$(free -m | awk '{if(NR==2) printf "%.1f",($2-$6-$NF)/1024}')
available=$(free -m | awk '{if(NR==2) printf "%.1f",($6+$NF)/1024}')
echo "内存 - 总大小:${total}G,使用:${used}G,剩余:${available}G"
}
memory
验证:
[root@logstash ~]# sh server_source_memory.sh
内存 - 总大小:3.8G,使用:0.3G,剩余:3.5G
[root@logstash ~]#
标签:printf,shell,服务器资源,free,used,memory,root,logstash
From: https://blog.51cto.com/ztj1216/11969533