03-统计内存

题目要求

写一个脚本计算一下linux系统所有进程占用内存大小的和。

xuehaozhe@ubuntu:~/shell$ free
              total        used        free      shared  buff/cache   available
Mem:        2030464      806448      154792        8484     1069224      975332
Swap:       2094076      342572     1751504

  
----------
xuehaozhe@ubuntu:~/shell$ ps aux |head
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.2 185164  5704 ?        Ss   Jun17   0:21 /lib/systemd/systemd --system --deserialize 29
root          2  0.0  0.0      0     0 ?        S    Jun17   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    Jun17   0:01 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   Jun17   0:00 [kworker/0:0H]
root          7  0.0  0.0      0     0 ?        S    Jun17   0:23 [rcu_sched]
root          8  0.0  0.0      0     0 ?        S    Jun17   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        S    Jun17   0:00 [migration/0]
root         10  0.0  0.0      0     0 ?        S    Jun17   0:00 [watchdog/0]
root         11  0.0  0.0      0     0 ?        S    Jun17   0:00 [watchdog/1]
  
xuehaozhe@ubuntu:~/shell$ ps aux |grep -v 'TIME COMMAND'|head 过滤掉第一行
xuehaozhe@ubuntu:~/shell$ ps aux |sed '1d' |head

root          1  0.0  0.2 185164  5704 ?        Ss   Jun17   0:21 /lib/systemd/systemd --system --deserialize 29
root          2  0.0  0.0      0     0 ?        S    Jun17   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    Jun17   0:01 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   Jun17   0:00 [kworker/0:0H]
root          7  0.0  0.0      0     0 ?        S    Jun17   0:23 [rcu_sched]
root          8  0.0  0.0      0     0 ?        S    Jun17   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        S    Jun17   0:00 [migration/0]
root         10  0.0  0.0      0     0 ?        S    Jun17   0:00 [watchdog/0]
root         11  0.0  0.0      0     0 ?        S    Jun17   0:00 [watchdog/1]
root         12  0.0  0.0      0     0 ?        S    Jun17   0:00 [migration/1]
  
  
#!/bin/bash
sum=0
for n in `ps aux |grep -v 'TIME COMMAND'|awk '{print $6}'`
do
    sum=$[$sum+$n]
done
echo $sum

你可能感兴趣的:(03-统计内存)