Skip to main content

Managing Processes (ps, top, kill, htop)

A process is an instance of a running program. When you execute a command, the system creates a process, which is assigned a unique Process ID (PID). Managing processes effectively is crucial for system monitoring, performance optimization, and troubleshooting.

Commands Covered

In this lesson, we will cover the following commands to manage processes in Linux:

  • ps – View process information.
  • top – Monitor real-time processes.
  • kill – Terminate processes.
  • htop – Interactive process viewer.

1. Viewing Processes with ps

The ps command provides a snapshot of all active processes at a given moment. Unlike top, it does not update in real-time.

Basic Syntax:

ps [options]

Commonly Used Options:

OptionDescription
-eDisplays all processes.
-fFull-format listing, including user and command details.
-u [user]Displays processes owned by a specific user.
-auxDisplays detailed information for all processes.

Examples:

1. Display all processes:

ps -e

2. Show detailed information for all processes:

ps -aux

Output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root 1 0.0 0.1 22576 2704 ? Ss 12:10 0:00 /sbin/init
user 1234 0.2 1.3 89672 13456 pts/0 S+ 12:11 0:02 /bin/bash

3. View processes for a specific user:

ps -u user

4. Find a specific process by name:

ps -aux | grep firefox

2. Monitoring Processes with top

The top command is used to monitor processes in real-time. It updates data continuously and is ideal for observing resource usage and system performance.

Basic Syntax:

top

Commonly Used Options:

KeyDescription
qQuit the top command.
kKill a process by entering its PID.
MSort processes by memory usage.
PSort processes by CPU usage.
rRenice a process (change priority).
zToggle color/monochrome display.

Examples:

1. View real-time processes:

top

2. Sort by memory usage:

Press M while in top.

3. Sort by CPU usage:

Press P while in top.

4. Kill a process directly in top:

Press k and enter the PID.


3. Terminating Processes with kill

The kill command is used to terminate processes using their PID.

Basic Syntax:

kill [signal] PID

Common Signals:

SignalNumberDescription
SIGTERM15Gracefully terminate a process (default).
SIGKILL9Forcefully terminate a process immediately.
SIGHUP1Restart the process.

Examples:

1. Gracefully terminate a process:

kill 1234

2. Forcefully kill a process:

kill -9 1234

3. Kill all processes by name:

killall firefox

4. Send a restart signal:

kill -1 5678

4. Interactive Process Management with htop

The htop command provides an interactive and user-friendly interface for monitoring and managing processes. Unlike top, it supports scrolling and visual representation of system resources.

Installing htop:

sudo apt install htop     # Debian/Ubuntu
sudo yum install htop # CentOS/RHEL

Launching htop:

htop

Key Features:

  • Color-coded resource usage.
  • Horizontal and vertical scrolling.
  • Search/filter processes.
  • Mouse support for process selection.

Keyboard Shortcuts:

KeyAction
F1Help menu.
F2Setup/configuration options.
F3Search for a process.
F4Filter processes based on criteria.
F5Tree view to show process hierarchy.
F9Kill a selected process.
F10Exit htop.

Examples:

1. View processes interactively:

htop

2. Search for a specific process:

  • Press F3 and type the process name.

3. Kill a process using htop:

  • Select the process with arrow keys and press F9.

Comparison of Tools

CommandReal-time MonitoringInteractive InterfaceProcess Control
psNoNoView only
topYesPartial (keyboard only)Yes
killNoNoYes
htopYesFully InteractiveYes

Key Takeaways:

  • ps is useful for listing processes at a specific point in time, ideal for scripting and logs.

  • top is used for real-time monitoring of processes, CPU, and memory usage.

  • kill allows you to terminate processes by sending signals.

  • htop provides an interactive, user-friendly process management tool with advanced filtering and sorting options.