To view the memory scripts consumed by the process

Source: Internet
Author: User

The script reads as follows:

#!/bin/sh  

mem=`free -m | awk 'NR==2{print $2}'`  
ps -aux 2>&1 | sort -k 4 -r | awk '$4 ~ /^[0-9]/ && $4>0 {print $4,$11}' | awk '{print $1/100*mem"   "$2}' mem=$mem | sort -k 2 | awk '  
{  
   a[$2] += $1;  
   b[$2]++;  
   total += $1;  
   total++;  
}  
END{  
  for(i in a){  
    t=i;  
    gsub(/:|.*\//, "", t);  
    printf "%10s   %s\n" ,a[i]"MB", t"["b[i]"]";  
  }  
print "Memory Total: "mem"MB, used: "total"MB, free: "mem-total"MB."  
}' mem=$mem | sort -n -r

Execution Result:

root@postgresql:~# sh cpu.sh 
  118.53MB   mysqld[1]
  67.167MB   apache2[6]
  23.706MB   POSTGRES[1]
  11.853MB   snapd[1]
   7.902MB   systemd-journald[1]
   7.902MB   sshd[2]
   3.951MB   systemd-udevd[1]
   3.951MB   systemd[1]
   3.951MB   sshd[1]
   3.951MB   POSTGRES[1]
   3.951MB   polkitd[1]
   3.951MB   init[1]
   3.951MB   -bash[1]
   3.951MB   Accounts-daemon[1]
Memory total:3951mb, USED:288.668MB, FREE:3662.33MB.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.