Linux··5 min

Process Management

Every program running on your system is a process. Understanding how Linux manages processes — how to inspect them, control them, and kill them — is essential for anyone working with Linux seriously.

Every program running on your system is a process. Your terminal is a process. Your browser is a process. The SSH daemon waiting for connections in the background is a process. Understanding how Linux manages all of these is essential for working with the OS seriously.

What is a process?

When you run a program, the kernel creates a process for it. That process gets a unique identifier called a PID — Process ID. Every process also has a PPID — Parent Process ID — because every process was started by another process.

The first process on any Linux system is systemd (or init on older systems). It has PID 1. Every other process on the system is a descendant of it.

Viewing processes

ps

ps shows a snapshot of currently running processes.

ps

By default it only shows processes in your current terminal session. To see everything:

ps aux
  • a — show processes from all users
  • u — show the user that owns each process
  • x — include processes not attached to a terminal

The output looks like this:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 168548 11320 ?        Ss   09:00   0:01 /sbin/init
jan       1234  0.1  0.5 123456 45678 pts/0    Ss   09:05   0:00 bash
jan       5678  2.3  1.2 234567 98765 pts/0    R+   09:10   0:03 firefox

The STAT column shows the process state:

  • R — running
  • S — sleeping (waiting for something)
  • Z — zombie (finished but not yet cleaned up by parent)
  • T — stopped

pgrep

Instead of grepping through ps output, use pgrep to find a process by name:

pgrep firefox

Returns the PID. Add -l to also show the name:

pgrep -l firefox

top and htop

top gives you a live, updating view of running processes sorted by CPU usage:

top

htop is the same but with a much more readable interface and mouse support. Install it if it is not already there:

sudo apt install htop
htop

In htop, F6 sorts by a column, F9 opens the kill menu, F10 exits.

Foreground and background jobs

When you run a command in the terminal, it runs in the foreground. Your terminal is blocked until it finishes. To run a command in the background, add &:

sleep 60 &

The shell prints the job number and PID, then returns the prompt immediately.

[1] 4821

To see your background jobs:

jobs

To bring a background job back to the foreground:

fg %1

To send a running foreground process to the background, press Ctrl + Z to suspend it, then:

bg %1

Signals and kill

Signals are how you communicate with processes. The most common use is stopping them.

kill PID

By default kill sends the SIGTERM signal (signal 15), which asks the process to terminate gracefully. The process can catch this signal and clean up before exiting.

If a process is not responding to SIGTERM:

kill -9 PID

-9 is SIGKILL. This cannot be caught or ignored by the process. The kernel terminates it immediately. Use this as a last resort — the process has no chance to clean up.

To kill by name instead of PID:

pkill firefox

Common signals:

SignalNumberMeaning
SIGTERM15Graceful termination (default)
SIGKILL9Immediate termination
SIGHUP1Hangup — often used to reload config
SIGSTOP19Pause a process
SIGCONT18Resume a paused process

Process priority — nice and renice

Every process has a priority value called niceness, ranging from -20 (highest priority) to 19 (lowest priority). The default is 0.

Start a process with a lower priority:

nice -n 10 ./my-script.sh

Change the priority of a running process:

renice -n 5 -p PID

Only root can set negative nice values (higher than default priority).

/proc

Every process has a corresponding directory in /proc named after its PID. This directory contains detailed information about the process as virtual files.

cat /proc/1/status

Useful files inside a process directory:

  • status — human readable process info
  • cmdline — the command that started the process
  • environ — environment variables
  • fd/ — open file descriptors
ls /proc/1/fd

Shows every file the process currently has open.


Understanding processes is what separates someone who uses Linux from someone who can actually administer it. When a system is slow, you look at processes. When something is unresponsive, you send signals. When something is consuming too much CPU, you renice it.

Next up: networking fundamentals — IP addresses, interfaces, and the tools to inspect and debug them.

0 views