When a Linux server suddenly spikes in temperature, fans start spinning wildly, and websites hosted on the machine begin timing out, you need to instantly identify which application is destroying your CPU. In a graphical desktop environment, you would open the System Monitor. However, on a headless server, the universal standard for diagnosing performance issues is the top command. This command transforms your terminal into a live, constantly updating dashboard that displays exactly what every process on your system is doing at any given second.
Basic Navigation of the top Interface
Unlike standard terminal commands that print text and exit, top takes over your entire screen until you explicitly tell it to quit.
- Open your Linux terminal.
- Type the command:
top - Press Enter.
The screen is divided into two main sections. The top section (the header) provides a macro overview of the system’s health, including the current server uptime, the number of logged-in users, the 1-minute, 5-minute, and 15-minute load averages, and a summary of total memory/swap usage.
The bottom section is the actual process list. By default, it updates every 3 seconds and is automatically sorted so that the process consuming the most CPU power is pushed to the absolute top of the list.
Sorting by CPU or Memory Usage
While the default CPU sorting is useful for identifying runaway scripts, a server crash is often caused by a memory leak (an application hoarding RAM until the system runs out and freezes). You can re-sort the live dashboard using keyboard shortcuts.
- Sort by Memory: Press Shift + M (Capital M). The list will instantly re-sort, bringing the application consuming the most RAM to the top.
- Sort by CPU: Press Shift + P (Capital P). This returns the list to the default view, prioritizing processor usage.
- Sort by Running Time: Press Shift + T (Capital T). This sorts the list based on which process has been running the longest continuously.
Killing a Rogue Process from Inside top
If you identify a process that is clearly misbehaving (e.g., a stalled PHP script eating 100% of a core), you do not need to exit top and run a separate kill command. You can terminate it directly from the dashboard.
- Look at the very first column of the
topoutput (labeled PID). Note the numeric ID of the rogue process (e.g.,14502). - While
topis still running, press the `k` key on your keyboard. - A prompt will appear just below the header saying:
PID to signal/kill [default pid = X]: - Type the PID of the rogue process (
14502) and press Enter. - It will ask what signal to send (default is 15, which is a graceful termination). Press Enter again.
The process will be instantly terminated and will vanish from the live list.
To safely exit the top dashboard and return to your standard bash prompt, simply press the `q` key.