Question

How to find the processes that are consuming the most server resources?

Posted November 12, 2019 2.3k views
Linux BasicsLinux Commands

Recently I’ve been getting this question quite a lot so I decided to share my thoughts with the community!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

One of the tools that I use to do so is the top command. Thye top command works quite well, all you need to do is to run top in your terminal and then hit one of the following keys to sort the processes by the information that you are looking for:

  • M - Sorts by current resident memory usage
  • P - Sorts by current CPU usage (this is the default)
  • ? - Displays a usage summary for all top commands

This is very important information to obtain when troubleshooting a slow server or a computer. This would help you make a decision on what processes to kill/software to uninstall.

An alternative of top is htop. It is an interactive system-monitor process-viewer and process-manager. It is designed as an alternative to the Unix program top. Some of the benefits of htop is that it comes with colors and is interactive, meaning that you could scroll the process list horizontally and vertically using the arrow keys, kill a process by pressing the F9’, sort the processes by Memroy/CPU usage and etc. by just clicking with your mouse.

Htop

Hope that this helps!
Regards,
Bobby

  • Recently we had a random issue where CPU shot up for a reasonable long period. We had monitoring set up and DO sent us alert as expected but unfortunately we missed that email and did not get a chance to top command.

    Is it possible to get top snapshot as part of that email?

    Or do you recommend any other setup.

    Thank you!

    • Hi there @AniruddhaPimpalkhare,

      What I could suggest is either setting up a monitoring solution like Netdata for example:

      https://www.digitalocean.com/community/tutorials/how-to-set-up-real-time-performance-monitoring-with-netdata-on-ubuntu-16-04

      Another thing that you could suggest is installing sysstat which would gather some useful information for you.

      To do that just run the following command:

      sudo apt install sysstat
      

      After that edit the /etc/default/sysstat fiel and set the following to true:

      ENABLED="true"
      

      Finally start the service and enable it:

      sudo systemctl start sysstat
      sudo systemctl enable sysstat
      

      To test if the command works, just run:

      • sar

      Then you could also do is to use the following script and set it run as a cron job for a couple of days:

      #!/bin/bash
      
      timeNow=$(date "+%F_%H:%M")
      
      filename="/root/processes_logs/log_${timeNow}.log"
      
      if [ ! -d "/root/processes_logs" ]; then
          mkdir /root/processes_logs
      fi
      
      touch ${filename}
      
      printf " ### Time of scan: $(date +%F_%H:%M) ### \n\n" >> ${filename}
      
      ###
      printf "\n ################## RAM ################## \n" >> ${filename}
      
      printf "\n ### Current Memory Usage: ### \n\n" >> ${filename}
      free -tm >> ${filename}
      
      printf "\n ### Top 10 processes by RAM usage: ### \n\n" >> ${filename}
      ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 10 >> ${filename}
      
      printf "\n ### RAM usage history for the last hour ###: \n\n" >> ${filename}
      sar -r -s $(date "+%H" -d "1 hour ago"):$(date "+%M"):00 -e $(date "+%H"):$(date "+%M"):00 >> ${filename}
      
      printf "\n ### Swap usage history for the last hour: ### \n\n" >> ${filename}
      sar -S -s $(date "+%H" -d "1 hour ago"):$(date "+%M"):00 -e $(date "+%H"):$(date "+%M"):00 >> ${filename}
      
      ###
      printf "\n\n ################## CPU ################## \n" >> ${filename}
      
      printf "\n ### Current load: ### \n\n" >> ${filename}
      uptime | cut -d' ' -f10-16 >> ${filename}
      
      printf "\n ### Sar history for last hour: ### \n\n" >> ${filename}
      sar -s $(date "+%H" -d "1 hour ago"):$(date "+%M"):00 -e $(date "+%H"):$(date "+%M"):00 >> ${filename}
      
      ###
      printf "\n\n ################## Disk: ################## \n" >> ${filename}
      df -h >> ${filename}
      
      ###
      printf "\n\n ################## Apache ################## \n" >> ${filename}
      
      printf "\n ### IPs Connected to the server: ### \n\n" >> ${filename}
      netstat -tn 2>/dev/null | grep :80 | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head >> ${filename}
      echo >> ${filename}
      
      ###
      printf "\n\n ################## MySQL ################## \n" >> ${filename}
      
      printf "\n ### MySQL uptime: ### \n\n" >> ${filename}
      mysqladmin ver | grep Uptime >> ${filename}
      
      printf "\n ### MySQL connections: ### \n\n" >> ${filename}
      mysql -e "show status like '%onn%';" >> ${filename}
      
      printf "\n ### MySQL processlist: ### \n\n" >> ${filename}
      mysql -e "show full processlist;" >> ${filename}
      

      I’ve used this script in the past, it could be updated a little bit but it should provide you with some useful information.

      Don’t forget to remove the cron job once you’ve gathered enough information.

      Hope that this helps!
      Regards,
      Bobby

      by Scarlz
      Netdata provides accurate performance monitoring through extensible web dashboards that visualise the processes and services on your Linux systems. It monitors metrics about CPU, memory, disks, networks, processes, and more. The steps in this tutorial cover everything you need to successfully set up one Ubuntu 16.04 server running Netdata using its built-in web server or, optionally, using Nginx.
Submit an Answer