>_ IzzyLab

Hands-on task

This task is step 3 of the course “Server: first steps”

Who is eating the CPU

easy~5 min

The website has slowed down. Three background programs run on the server: thumbnailer, indexer and notifier. One of them is stuck in an infinite loop and has taken the whole CPU.

Find out which one and stop just that one. The other two work fine — leave them alone.

What you'll practise:
  • top
  • ps --sort
  • %CPU
  • kill
  • CPU load

Topic breakdown

When a server slows down, the first thing to check is who's taking the CPU. That's what top is for — a live table of processes that refreshes every few seconds:

  PID USER      PR  NI    VIRT    RES  %CPU  %MEM     TIME+ COMMAND
 2210 www-data  20   0  812340  95120  48.3   2.4   3:12.07 php-fpm
  731 postgres  20   0  402116  31544   3.0   0.8   0:41.55 postgres

The key columns: PID is the process number, %CPU is the share of the CPU it's using, %MEM its share of memory, TIME+ how much CPU time it has used in total, COMMAND its name. At the top of top there's also a summary: load average — the average load over 1, 5 and 15 minutes. Press q to quit.

top is handy for eyeballing, and for scripts and quick checks there's ps with sorting:

ps aux --sort=-%mem | head -5    # the five hungriest by memory

Once you've found the culprit, stop it by PID with kill.

What's checked

  1. The program that was taking the CPU is stopped
  2. The other two programs keep running

What it looks like