Kahibaro
Discord Login Register

2.6 Working with Processes

Understanding Processes on Linux

A process is a running instance of a program. Every command you start from the terminal, every background service, and every graphical application is a process.

Linux assigns each process a PID (Process ID), a number that uniquely identifies it while it is running. Processes also have:

In this chapter you’ll learn how to see, understand, and influence processes from the command line.


Basic Process Concepts

Process vs program vs command

A single program can create many processes (for example, a web browser spawns helper processes).

Foreground and background

From an interactive shell:

Foreground and background control for your own shell session is covered in detail in the dedicated “Foreground and background jobs” chapter; here we focus on processes system-wide.


Viewing Processes

There are many tools to view processes. For beginners, the most important are ps, top, and htop.

Listing processes with `ps`

The ps command shows a snapshot of running processes.

Common usages:

ps                # processes in the current shell session
ps x              # your processes, including those without a terminal
ps aux            # all processes (BSD-style options)
ps -ef            # all processes (Unix-style options)

Some commonly used columns (you may see a subset depending on options):

Example:

ps aux | head

Typical output line:

root       1  0.0  0.1 167808  8844 ?        Ss   10:00   0:02 /sbin/init

Here:

Filtering processes with `ps`

You can combine ps with grep to locate processes:

ps aux | grep ssh
ps -ef | grep firefox

Include grep carefully: it will show up as its own line. You can filter it out, for example:

ps aux | grep ssh | grep -v grep

Another approach is using pgrep:

pgrep ssh        # show PIDs of processes whose name matches 'ssh'
pgrep -a ssh     # show PIDs and full commands

`top`: real-time process viewer

top shows processes and system usage in real time. Run it simply as:

top

Key parts of the display:

Common keys inside top:

You can limit how many lines are shown by combining with head is not possible because top is interactive; instead, use non-interactive tools like ps if you need simple one-shot output.


`htop`: a friendlier `top` (if installed)

htop is not always installed by default, but many distributions provide it.

Key differences from top:

Typical keys:

Depending on your system, you may need to install it, for example:

sudo apt install htop    # Debian/Ubuntu
sudo dnf install htop    # Fedora/RHEL

Understanding Process States and Status

Process state or STAT (from ps) tells you what a process is currently doing.

Common STAT characters:

Additional flags can be appended, such as:

Example:

ps -o pid,stat,cmd -p 1

Output might look like:

  PID STAT CMD
    1 Ss   /sbin/init

Process IDs and Parent/Child Relationships

Every process has:

To see this:

ps -o pid,ppid,cmd | head

Example:

  PID  PPID CMD
    1     0 /sbin/init
  723     1 /usr/lib/systemd/systemd-udevd
 1550  1200 /usr/bin/bash
 1601  1550 /usr/bin/python3 script.py

You can also show a process tree:

ps axjf          # tree format (style depends on distro)

or, if installed:

pstree
pstree -p       # show PIDs
pstree -u       # show usernames

Parent-child relationships matter because:

Finding Processes by Name or Other Attributes

Beyond ps | grep, there are more specialized tools.

`pgrep` and `pkill` (for matching by name)

pgrep lists process IDs by name:

pgrep ssh
pgrep -u youruser bash      # all bash processes owned by 'youruser'
pgrep -a firefox            # include the command line

pkill sends signals to processes matching a pattern (full details about signals in “Killing processes”):

pkill firefox          # send default signal (TERM) to all firefox processes
pkill -u youruser bash # signal all bash processes owned by youruser

Use pkill with care, as it can affect multiple processes at once.

`ps` with filters

ps can filter by user, PID, etc.:

ps -u youruser               # processes owned by youruser
ps -p 1234 -o pid,user,cmd   # specific PID with selected columns

Monitoring Resource Usage Per Process

Often you want to know which processes are using the most CPU or memory.

Using `ps` to see top consumers

List all processes and sort by memory:

ps aux --sort=-%mem | head

Sort by CPU:

ps aux --sort=-%cpu | head

The first few lines will be the heaviest users.

Using `top` or `htop` interactively

This is useful to quickly identify “what is making the fan spin” or “what is using all my RAM”.


Process Priorities and Niceness (Overview)

Linux uses a scheduler to share the CPU between processes. Each process has:

Niceness ranges from $-20$ (highest priority) to $19$ (lowest priority). Counterintuitively:

You can see the nice value with:

ps -o pid,ni,cmd | head

The details of changing niceness (nice, renice) and advanced resource control are typically considered a more intermediate topic; for basic usage, it’s enough to know that:

Inspecting Details of a Single Process

Sometimes you want to know everything about one particular process.

Using `ps` with `-p`

For one PID:

ps -p 1234 -o pid,user,%cpu,%mem,stat,etime,cmd

Where:

Looking into `/proc`

Linux exposes process information through the /proc pseudo-filesystem. For a process with PID 1234, you’ll see a directory /proc/1234 (while it’s running) containing many files.

Some useful ones:

Example:

cat /proc/1234/status
ls -l /proc/1234/fd

This is mainly for deeper debugging or curiosity; day-to-day, you’ll usually rely on ps, top, and tools built on top of /proc.


Practical Examples

Example: Finding which process is using port 80

Network details are covered in the networking chapters; here is a quick process-related use.

On modern systems, you can use ss or lsof:

sudo ss -ltnp 'sport = :80'

or:

sudo lsof -i :80

Both commands show which process (PID and name) is bound to port 80, so you can then inspect or manage that process with ps, top, or other tools.

Example: Checking what your terminal is running

To see what process is attached to your current shell’s terminal:

ps -t $(tty)

This shows the shell and any foreground/background processes using that terminal as their TTY.


Summary

In this chapter you learned how to:

You will build on this foundation in the dedicated chapters about killing processes, foreground and background jobs, and system services and daemons, where you’ll learn how to control, stop, and manage processes more actively.

Views: 157

Comments

Please login to add a comment.

Don't have an account? Register now!