Dealing with High CPU Usage on your WordPress Server

Content Error or Suggest an Edit

Notice a grammatical error or technical inaccuracy? Let us know, we will give you credit!

SSH and Linux Shell Knowledge Required

This article requires a basic understanding of connecting to a server through SSH and issuing Linux Shell commands using Bash.

Identifying High CPU Usage

This article will assist you in identifying high CPU usage on your server (VM, Baremetal) that hosts one or multiple WordPress sites.

Now, I won’t talk about identifying if your server has a problem with High CPU usage. You can usually figure this out from your provider’s usage graphs, control panel usage graphs, or alerting. For instance, GridPane provides an alert when your server suffers from a high CPU load.

1. Linux ps Command

This command lets you view all the processes and threads on a Linux system in a tree view. The default output of running the command provides limited information, so you’ll want to use the following commands.

List all Processes

ps -auxwwf
  • -a = Select all processes except both session leaders (see getsid(2)) and processes not associated with a terminal.
  • -u = Display user-oriented format.
  • -x = this option causes ps to list all processes owned by you (same EUID as ps), or to list all processes when used together with the a option.
  • -w = Wide output. Use this option twice for unlimited width.
  • -f = Do full-format listing.
Output of ps -auxwwf

List Top 15 CPU Processes

ps -auxwwf | sort -nrk 3,3 | head -n 15
  • We’re using the same ps -auxwwf as above.
  • sort -nrk 3,3
    • -n = numeric sort
    • -r = reverse the result of comparisons
    • -k = sort via a key; KEYDEF gives location and type
    • 3,3 = column 3, data 3
Output of the Linux ps command piped through sort and head.

2. Linux top and htop Command

Linux “top” command

The top command in Linux will provide you with a real-time view of the running processes on a system and refresh every 3 seconds. It’s a great tool when you quickly need to figure out what is going on resource-wise on a Linux system.

You can press “shift + ?” to get to the help screen, which provides instructions on how to control the output, including sorting based on fields such as %CPU

The top (table of processes) command shows a real-time view of running processes in Linux and displays kernel-managed tasks. The command also provides a system information summary that shows resource utilization, including CPU and memory usage

Linux top command
Linux top command help

Linux “htop” command

The Linux htop command is top on steroids, providing some improvements to the top command such as color, displaying of CPU cores/Memory as usage graphs.

Htop is an interactive real-time process monitoring application for Linux/Unix-like systems and also a handy alternative to top command, which is a default process monitoring tool that comes pre-installed on all Linux operating systems.

https://www.tecmint.com/htop-linux-process-monitoring
Linux ‘htop’ command output

Typically htop is whatever one recommends and uses due to its easy-to-read UI. However, there is one caveat, CPU steal isn’t displayed by default and has to be turned on.

This GitHub issue explains how to enable “Detailed CPU time”, and then how to change the CPU graph from bar to text. You can also add the “CPU Average” meter and change it to text as it’s smaller https://github.com/hishamhm/htop/issues/369

Here’s what it looks like once you make the change.

htop with detailed CPU time.

Another caveat is that you can’t display processes in tree view and sort on CPU usage, which is understandable.

Installing htop

The “htop” package is available on most modern Linux distributions as the package “htop”. For Ubuntu/GridPane folks you can install it using the following command

apt-get install htop

Tracking High CPU Usage

Now that you’re familiar with reviewing your server’s high CPU usage in real time, what about tracking which CPU usage over time? Enter atop

Linux ‘atop’ command

The Linux atop command is a small and efficient tool for logging snapshots of your system in a top-like format over time. Allowing you to review your server’s processes back in time.

Atop is an ASCII full-screen performance monitor which can log and report the activity of all server processes. One feature I really like is that atop will stay active in the background for long-term server analysis (up to 28 days by default).

https://haydenjames.io/use-atop-linux-server-performance-analysis/
atop command output

Installing atop

The “atop” package is available on most modern Linux distributions as the package “atop”. For Ubuntu/GridPane folks you can install it using the following command.

apt-get install atop

Configuring atop

Once atop is installed, you will need to ensure that it is running as a service and that it starts on boot if your server is rebooted.

systemctl enable atop
systemctl start atop

By default, atop will take a snapshot every 600 seconds; you may want to bring this down to 300 seconds which you can do so by running the following command.

sudo sed -i -e 's/INTERVAL=600/INTERVAL=300/g' /usr/share/atop/atop.daily
sudo systemctl restart atop

Netdata

You might have heard about Netdata; free software runs an agent on your server to collect operating system metrics. The agent reports back to the netdata.cloud where it’s processed, graphed out, and if there’s an issue, an email alert is sent out.

Changelog

None at this time.

19 Shares:
You May Also Like

GridPane Releases Limited API

GridPane API Documentation GridPane released an API, with limited functionality. You can review the API document here. https://documenter.getpostman.com/view/13664964/TVssjU7Z…