CodeGym /Courses /Docker SELF /Managing System Processes: ps, top, htop

Managing System Processes: ps, top, htop

Docker SELF
Level 2 , Lesson 4
Available

Managing System Processes: ps, top, htop

1. The ps command — a static look at processes

If Linux were a restaurant, a process would be the chef in the kitchen. It's an executable instance of a program that exists in memory. When you run an application or execute a file from the command line, a process is created that then carries out its tasks until completion.

From a programming perspective, processes are managed by the Linux kernel, and each has a unique identifier — PID (Process ID).

The ps (process status) command is a basic way to view a list of processes. It gives a static snapshot of the current processes in the system. Using ps, you can get info on running processes: their IDs, owners, command line, and resource usage.

Basic Syntax

Here's what the basic command looks like:

ps

This will show processes running in the current terminal.

But that's boring! Let's spice it up a bit:

ps aux

Here's what it does:

  • a — shows processes from other users.
  • u — adds a more human-readable format.
  • x — includes processes not tied to a terminal.

Example output:

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.1  22556  1044 ?        Ss   12:34   0:01 /sbin/init
student      2345  0.1  0.2  34567  2048 pts/0    S+   12:35   0:02 bash
student      4567  0.3  0.5  56789  5144 pts/0    R+   12:36   0:03 ps aux
  • USER: the user who started the process.
  • PID: the process ID.
  • %CPU: the percentage of CPU usage.
  • %MEM: the percentage of memory usage.
  • COMMAND: the command that triggered the process.

Advanced Options

  1. View a tree-like structure of processes:

    ps -ejH
    

    This helps to understand which processes are "children" of other processes.

  2. Filter by PID:

    ps -p 1234
    

    Where 1234 is your PID. It only shows the specified process.

  3. View all processes along with the full command line:

    ps -ef
    

2. Command top — real-time process monitoring

top is like a TV for processes. You can watch processes change in real time. If you're running a server or your computer starts "lagging," top is your best buddy. With it, you can see which processes are hogging the most CPU and memory.

Basics of using it

Just type in the terminal:

top

Example output:

  • The top line shows the system "load."
  • The middle section tells you about tasks (total, active, sleeping, zombie).
  • The lower section is all about memory.
  • The bottom block is a list of processes with lots of useful info, including PID, USER, %CPU, and COMMAND.

Navigation in top

  • Press k to kill a process. Enter the PID.
  • Press P to sort processes by CPU usage.
  • Press M to sort by memory usage.
  • Press q to quit.

3. The htop Utility — A User-Friendly Interface

If top seems too "raw" for you, welcome to htop. It's a visual interface for process monitoring. It's colorful, supports the mouse, and is super intuitive.

Installation

If htop isn’t installed, add it:

sudo apt-get install htop # For Debian-based distributions
sudo yum install htop     # For RedHat-based distributions

Execution

To run htop, just enter:

htop

Sample interface:

Advantages of htop:

  • Ability to scroll through the process list.
  • Color-coded highlighting for CPU, memory, swap.
  • Convenient keys for killing processes (like F9).

4. Practical Assignment

Task 1: Find the Most "Greedy" Process

  • Open top or htop.
  • Sort the processes by CPU usage and find out its PID.

Task 2: Terminate the Process

  • Use kill to "kill" the process:
    kill <PID>
    

Task 3: Process Analysis

  • Use ps to analyze the process PID:
    ps -p <PID> -o pid,user,%cpu,%mem,cmd
    

Gotchas and Recommendations

  1. Don't kill processes unless you're sure they aren't critical to the system. For instance, the process with PID 1 is init, the foundation of the OS.
  2. The htop utility needs to be installed separately if it isn't already. If the command doesn't work, check the installation.
  3. Be cautious with permissions. Terminating processes started by another user might require administrator rights.

In practice, you'll see how these tools make working with Linux incredibly flexible and straightforward. Now you're the boss of processes!

1
Task
Docker SELF, level 2, lesson 4
Locked
Viewing current processes
Viewing current processes
1
Task
Docker SELF, level 2, lesson 4
Locked
Tree structure of processes
Tree structure of processes
1
Task
Docker SELF, level 2, lesson 4
Locked
Identifying the most resource-intensive process
Identifying the most resource-intensive process
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION