GoDaddy Hilfe

Review CPU and memory on your Linux server

Resources like CPU and memory (RAM) are an integral part of your site's functionality. If your code uses too much, the site can become slow or stop working altogether. All other processes (like email) share the same resources with the sites on your server. Any process taking more than their (expected) fair share, can take the server down completely.

Why would I need to check resource usage?

  • Your database driven site (like WordPress) is slow.
  • Email is taking a long time to arrive.
  • Your site displays an error or just spins
  • Tasks performed on your server are taking longer than normal (like backups).

How do I check resource usage?

Linux offers several tools to investigate CPU and memory usage like top, sar, and watch.

The top command allow you to view system tasks running in real-time. You can see overall info like uptime, load average, CPU and memory usage. The -c switch allows you to see program name under COMMAND. For a full list of shortcut keys, use man top in SSH.


[root@server ~]$ top -c
top - 15:08:23 up 115 days,  6:37,  2 users,  load average: 1.76, 1.35, 1.13
Tasks: 330 total,   1 running, 326 sleeping,   0 stopped,   3 zombie
Cpu(s):  0.7%us,  2.9%sy,  0.0%ni, 81.5%id, 14.9%wa,  0.0%hi,  0.1%si,  0.0%st
Mem:  32754672k total, 28808088k used,  3946584k free,   321260k buffers
Swap:  8388604k total,    25992k used,  8362612k free,  8140560k cached

    PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
   6926 mysql      20   0  130g  16g  22m S 12.0 52.2  27065:56  /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin
   2454 td-agent  20   0 1210m 160m 2440 S  0.7  0.5 347:19.48 /opt/td-agent/embedded/bin/ruby -Eascii-8bit:ascii-8bit /

The System Activity Reporter (sar) is a tool to help you monitor your server's usage statistics. Sar is part of the sysstat package. Using the command sar alone will give you statistics for the last 24 hours.

sar -f allows you to view historic log files starting at 12:00 a.m. for the day.


[root@server ~]$ sar -f /var/log/sa/sa10
Linux 2.6.32-042stab141.3 (server-hostname.net) 01/10/2022 _x86_64_  (24 CPU)

12:00:01 AM     CPU     %user     %nice   %system   %iowait    %steal     %idle
12:10:01 AM     all      1.92      0.00      0.39      0.00      0.00     97.69
12:20:01 AM     all      1.63      0.00      0.34      0.00      0.00     98.02
12:30:01 AM     all      1.64      0.00      0.41      0.01      0.00     97.94
12:40:01 AM     all      2.85      0.00      0.61      0.00      0.00     96.54
12:50:01 AM     all      2.71      0.00      0.57      0.01      0.00     96.72
01:00:01 AM     all      2.02      0.00      0.38      0.00      0.00     97.60
01:10:01 AM     all      1.43      0.00      0.30      0.00      0.00     98.27

sar -r will display free and used memory stats


[root@server ~]$ sar -r
Linux 2.6.32-042stab141.3 (server-hostname.net) 02/02/2022  _x86_64_  (24 CPU)

12:00:01 AM kbmemfree kbmemused  %memused kbbuffers  kbcached  kbcommit   %commit
12:10:01 AM  99454800  32354304     24.55   1429856  22624260   8285744      5.91
12:20:01 AM  99760972  32048132     24.31   1429884  22631184   7960428      5.68
12:30:01 AM  99751920  32057184     24.32   1429920  22638392   7974804      5.69
12:40:01 AM  99638056  32171048     24.41   1429960  22643192   8035000      5.73
12:50:01 AM  99752696  32056408     24.32   1430012  22644804   7891100      5.63

The Linux watch allows you to watch commands or tasks as they complete or execute. watch "mysql -e'show full processlist;'" will show all running mysql queries, updating every 2 seconds.


Every 2.0s: mysql -e'show full processlist;'                                                                                                                                                                         Sat Feb 12 11:09:56 2022
Id      User    Host    db      Command Time    State   Info
26574057        mysql_db1    localhost       mysql_db1      Query   2       Sending data    SELECT * from db_data_main where DOT_NUMBER = 1381207
26574063        mysql_db1    localhost       mysql_db1      Query   1       Sending data    SELECT * from db_data_main where DOT_NUMBER = 3173380
26574064        mysql_wp3    localhost       mysql_wp3 Sleep   0               NULL
26574068        root    localhost       NULL    Query   0       init    show full processlist

Next Steps

High resource usage can be a one-off caused by malicious traffic or a hung process (like a large backup). Frequent high resource usage not caused by traffic or hung process can only be resolved through optimization or upgrade. Keep in mind if you do not optimize your server and sites, an upgrade is only a temporary fix.

Signs it may be time to optimize (server and site(s))

  • Not caused by malicious traffic
  • Site code is outdated/generating deprecated errors
  • CPU usage is high but memory usage is low.
  • MySQL queries lag when traffic increases

Signs it may be time to upgrade

  • Traffic to your site(s) is steadily increasing
  • The server has been optimized for your site needs
  • You have optimized your site(s) by eliminating long mysql queries and update the code to support newer versions of PHP.
  • You are using caching (like Memcache) but the load remains high