Quick Tip: Get on ‘top’ of VPS Processes

Whether you’ve just noticed some sluggishness on your VPS or you’ve run into a situation where you’re using more than your fair share of your shared resources, there might come a time when you need to figure out which processes are taking up the most CPU/RAM.

Linux installations come with a bevy of tools that are acceptable for this purpose, but none are quite as universal as top. Or, for those who like a little more interactivity, htop.

By default, top displays the most CPU intensive processes currently running on your VPS. It will update the list every 3 seconds so that you can understand if a process is spiking CPU usage every so often or is a consistent hog.

You can use a number of different options while running top to customize its output.

Specify the refresh period, in seconds. The below command will change the period to 10 seconds.

top -d 10

Sort by specific column. First, type top -O to see which columns are available to you, then use top -o to specify. For example, you can search by TIME to see which processes have been running longest.

top -o TIME

Or based on CPU usage.

top -o %CPU

Show processes from a specific user. Could be useful for those with more complex deployments.

top -u joel

Show only certain process IDs.

top -p 12345

Even when top is running, there’s a lot you can do to customize or drill down into the output.

dChange the refresh rate.
zTurn on/off colors.
lHide/display the load average (top row).
tChanges how the tasks are displayed.
mChanges how memory usage is displayed.
1Switch between one/many CPU mode.
RReverse the sorting order.
cDisplay the absolute path of the process.
kKill the process by its PID.
PSort processes by CPU utilization, high to low.
MSort processes by RAM utilization, high to low.
nLimit the number of processes shown.

Finally, you can add new fields to the top output. Press f once top is running, which will display all your options. Use the arrow keys to navigate around, and then hit d to enable/disable a given field. Enabled fields will have an asterisk next to them, and will be bolded.

By combining these techniques together, you can instantly get a handle on your most taxing processes. And then you can start using htop to dive even deeper.

Free ebook for securing your servers