Linux查看Swap内存使用情况

查看指定进程swap使用情况

# cat /proc/3315/smaps   | grep Swap  | more
Swap:                  0 kB
SwapPss:               0 kB
Swap:                  4 kB
SwapPss:               4 kB

编写脚本getswap.sh,查看所有进程的swap,并排序

echo -e "pid\tswap\tproc_name"
for pid in $(ls -l /proc | grep ^d | awk '{ print $9 }'| grep  -v [^0-9]); do
    # 判断改进程是否占用了swap
    grep -q "Swap" /proc/$pid/smaps 2>/dev/null
    if [ $? -eq 0 ];then
    # 如果占用了swap
        swap=$(grep Swap /proc/$pid/smaps | gawk '{ sum+=$2;} END { print sum }')
        proc_name=$(ps aux | grep -w "$pid" | grep -v grep  | awk '{ for(i=11;i<=NF;i++){ printf("%s ",$i); }}')
        if [ $swap -gt 0 ];then
        # 输出swap信息
            echo -e "$pid\t${swap} KB\t$proc_name"
        fi
    fi
    done | sort -k2 -n

查看结果

 # sh getswap.sh
pid     swap    proc_name
1019    8 KB    /usr/bin/VGAuthService -s
27855   8 KB    sshd: root@pts/1
27979   48 KB   sshd: root@notty
1026    224 KB  /usr/sbin/NetworkManager --no-daemon
1259    236 KB  (sd-pam)
3315    280 KB  /usr/sbin/sshd -D [email protected],[email protected],aes256-ctr,aes256-cbc,[email protected],aes128-ctr,aes128-cbc [email protected],[email protected],[email protected],[email protected],hmac-sha2-256,hmac-sha1,[email protected],hmac-sha2-512 -oGSSAPIKexAlgorithms=gss-gex-sha1-,gss-group14-sha1- -oKexAlgorithms=curve25519-sha256,[email protected],ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256,diffie-hellman-group14-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1 -oHostKeyAlgorithms=rsa-sha2-256,[email protected],ecdsa-sha2-nistp256,[email protected],ecdsa-sha2-nistp384,[email protected],rsa-sha2-512,[email protected],ecdsa-sha2-nistp521,[email protected],ssh-ed25519,[email protected],ssh-rsa,[email protected] -oPubkeyAcceptedKeyTypes=rsa-sha2-256,[email protected],ecdsa-sha2-nistp256,[email protected],ecdsa-sha2-nistp384,[email protected],rsa-sha2-512,[email protected],ecdsa-sha2-nistp521,[email protected],ssh-ed25519,[email protected],ssh-rsa,[email protected] -oCASignatureAlgorithms=rsa-sha2-256,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,rsa-sha2-512,ecdsa-sha2-nistp521,ssh-ed25519,ssh-rsa
1042    1160 KB /usr/libexec/platform-python -Es /usr/sbin/tuned -l -P
3322    6108 KB /usr/sbin/rsyslogd -n
773     6108 KB /usr/lib/systemd/systemd-journald


你可能感兴趣的:(Linux查看Swap内存使用情况)