If the server is responding slowly, the first question is always what is causing the load. The top and htop commands display running processes in real time, along with how much CPU time and memory they are using. Both are useful for identifying a runaway script, a database consuming too much memory or a sudden spike in traffic.
The ‘top’ command
The top command is available by default on almost all Linux systems. Run it in the terminal:
top
The header
The top part of the screen displays summary data:
- load average: the system’s average load over the last 1, 5 and 15 minutes. For an explanation of this, see the article ‘What does load mean on Linux systems?’.
- %CPU(s): the distribution of processor time.
usrefers to user programs,syto the kernel,idto idle time,wato disk wait time, andston a VPS refers to the time the virtual machine spent waiting for the physical processor. - MiB Mem and MiB Swap: the total, free, used and cached memory, as well as swap space.
The list of processes
Below this, a list of processes is displayed, sorted by CPU usage by default. The most important columns are: PID (process identifier), USER (owner), RES (physical memory actually used), %CPU, %MEM, TIME+ (total processor time used) and COMMAND.
A process’s %CPU value can exceed 100 per cent: 100 per cent corresponds to one full processor core, so a process running on four cores can show a value of up to 400 per cent.
Useful keyboard shortcuts
P: sort by CPU usage (default)M: sort by memory usage1: processor cores individuallyc: display the full command line (for example, which PHP script is running)u: filtering a user’s processesk: stopping a process by specifying the PIDq: log out
In a script or for logging purposes, the command top -b -n 1 | head -n 20 outputs a single snapshot to the terminal.
The htop command
htop is a more colourful, clearer and easier-to-use version of top. It is not installed everywhere:
# Debian, Ubuntu
sudo apt install htop
# AlmaLinux, Rocky Linux (a htop az EPEL tárolóban van)
sudo dnf install epel-release
sudo dnf install htop
htop displays CPU and memory usage as bars; it can be controlled using the mouse, and processes can be managed using the function keys:
F3: search by nameF4: filteringF5: a fan view showing the relationship between parent and child processesF6: Selecting a sort criterionF9: sending a signal to the selected process, i.e. stopping it
A detailed explanation of the htopscreen, the other keys and the startup switches can be found in the article ‘Installing and using htop on AlmaLinux’.
Stopping a process
When shutting down, first send the ‘gentle’ SIGTERM signal (15), which gives the program the opportunity to shut down properly. Only if it does not respond to this should you use the SIGKILL signal (9). From the command line: kill 12345, or, as a last resort, kill -9 12345. Do not restart system services, such as the web server or the database, in this way; instead, use the systemctl command.
What should you look out for?
If memory usage is consistently close to the maximum and the swap space is also filling up, the server either needs more memory or a process is leaking memory. If the load continues to increase in line with traffic, it is worth switching to a larger KVM VPS plan. On shared cPanel hosting, you can monitor the load on your own account via the CloudLinux resource usage page.