Table of Contents
- Identifying High CPU or Memory Usage
- Tracking Resources over Time
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 or Memory 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
- -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.
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
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 “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
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.
Another caveat is that you can’t display processes in tree view and sort on CPU usage, which is understandable.
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 Resources over Time
Now that you’re familiar with reviewing your server’s high resource usage in real-time, what about tracking which resources over time? There are a number of tools and methods available of which I’ll detail below.
1. 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/
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
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
Viewing atop History
You can view the historic data of atop through SSH by running the atop command and providing an atop raw data file. The raw data files are located in /var/log/atop, you’ll see that there is a file generated for each day.
Once inside the /var/log/atop directory, you can then use the atop command with the -r switch to read an atop raw data file.
atop -r atop_20221123
As you can see from the screenshot above, quite a bit of data is shown. You’ll notice on the top left-hand side the date and time. You can move forward and backwards using the following keys:
- t – Show next.
- T (Shift +t) – Show previous.
If you have the date and time of the spike in resources, you can find out what processes were running at that time and hopefully find a culprit that caused the resource increase.
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 an email alert is sent out if there’s an issue.
You can install Netdata on pretty much any server, it’s pretty versatile. You visit the Netdata website, sign up and then add a node. You will be given a command to run on your server as root. There may be special considerations when using platforms like GridPane or Runcloud.
Installing Netdata on GridPane
Below is a guide on how to install Netdata on GridPane.
The guide doesn’t cover enabling MySQL, Nginx or PHP-FPM, which require additional server configuration.
GridPane System Users and Sites
You’ll want to ensure that every site has its own system user, not only because it’s important for security, you can then see each site’s CPU usage under User->CPU and identify if a single site is the cause of the high CPU usage.
Installing Netdata on Runcloud
I have not installed Netdata on Runcloud. However, it shouldn’t be much different than GridPane. You will likely need additional configuration to enable MySQL, Nginx and PHP-FPM.
- 03-31-2023 – Fleshed out Netdata section.
- 11-23-2022 – Renamed article to include memory as well as CPU. Added section on reading atop history.