Skip to content

Beginner · Processes, services and logs · topic 13 of 57 · 30 min

Processes: ps, top and jobs

Explanation

A process is a running program. Each has a PID (process ID), a parent, an owner and a state. ps shows a snapshot of processes, top shows a live view sorted by CPU, and the shell's job control (&, Ctrl+Z, bg, fg, jobs) lets you run commands in the background.

Programs and processes

A program is a file on disk, like /usr/sbin/httpd. A process is a running copy of it, with its own memory. One program can run as many processes: a busy web server may have dozens of httpd processes. The kernel gives each process a number, the PID. The first process started at boot is systemd, with PID 1; every other process has a parent (PPID) that started it.

Think of it like this: A recipe book is the program; a cook following one recipe right now is a process. Ten cooks can follow the same recipe at the same time, each with their own pot. The head chef (systemd, PID 1) hires the cooks, and each cook can hire helpers.

ps: a snapshot

bash
$ ps aux | head -4
USER   PID %CPU %MEM    VSZ   RSS TTY  STAT START   TIME COMMAND
root     1  0.0  0.4 174744 16884 ?    Ss   09:01   0:02 /usr/lib/systemd/systemd --switched-root --system
root     2  0.0  0.0      0     0 ?    S    09:01   0:00 [kthreadd]
root   842  0.0  0.2  15840  9100 ?    Ss   09:01   0:00 sshd: /usr/sbin/sshd -D [listener] ...
$ ps -ef --forest | grep -A2 sshd    # parent/child tree
$ ps -o pid,ppid,user,%cpu,%mem,cmd --sort=-%mem | head -5   # pick columns, sort by memory
$ pgrep -l httpd
1301 httpd
1302 httpd
ColumnMeaning
PID / PPIDProcess ID / parent's ID
%CPU, %MEMShare of CPU and of RAM
RSSMemory really in RAM, in KiB
TTYTerminal; ? means none (a service)
STATState: R running, S sleeping, D waiting on disk (cannot be interrupted), T stopped, Z zombie

A zombie (Z) is a finished process whose parent has not yet collected its exit status. A few zombies are harmless; thousands mean the parent program has a bug. You cannot kill a zombie; you fix or restart its parent.

top: a live view

bash
$ top
top - 10:42:01 up 3 days,  2:10,  2 users,  load average: 0.52, 0.48, 0.40
Tasks: 182 total,   1 running, 181 sleeping,   0 stopped,   0 zombie
%Cpu(s):  4.1 us,  1.2 sy,  0.0 ni, 94.5 id,  0.1 wa,  0.0 hi,  0.1 si,  0.0 st
MiB Mem :   3654.2 total,    512.4 free,   1820.6 used,   1321.2 buff/cache
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
 1301 apache    20   0  412844  20480   8192 S   2.3   0.5   0:12.44 httpd
Keys inside top: P sort by CPU, M sort by memory, k kill a PID, r renice, q quit.

The load average is the average number of processes running or waiting to run over the last 1, 5 and 15 minutes. Compare it with the number of CPUs (lscpu or nproc): a load of 4 on a 4-CPU server is busy but fine; 12 on 4 CPUs means work is queueing. High wa (I/O wait) in the CPU line means processes are waiting for the disk.

Jobs: foreground and background

bash
$ sleep 600 &            # & starts it in the background
[1] 5120
$ jobs
[1]+  Running                 sleep 600 &
$ tar -czf /tmp/etc.tgz /etc     # started in the foreground... press Ctrl+Z
^Z
[2]+  Stopped                 tar -czf /tmp/etc.tgz /etc
$ bg %2                   # continue job 2 in the background
$ fg %1                   # bring job 1 to the foreground (Ctrl+C would stop it)

Jobs belong to your shell. If you log out, they usually get a hang-up signal and stop. For a long task on a remote server, use a terminal multiplexer like tmux, or nohup command &, or better, run it as a systemd service or timer.

Common mistakes

  • ps aux | grep httpd also shows the grep line itself. Use pgrep httpd, or grep [h]ttpd.
  • Reading %MEM in top as "free memory is low, panic". Linux uses spare RAM as disk cache (buff/cache). Look at the "avail Mem" value in top or "available" in free -h instead.
  • Starting a 3-hour backup in an SSH session with & and then closing the laptop. Use tmux or a systemd unit.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Processes: ps, top and jobs · 1/6

PID · PPID · USER · STATE

A process is a running program. Each one has a PID, a parent, an owner and a state.

Prefer a video by a teacher? Find free videos on “linux processes ps top jobs bg fg explained” on YouTube ↗

In real life: Finding the memory hog during a flash sale

During a midnight flash sale, a Bengaluru e-commerce server becomes slow. The engineer opens top, presses M to sort by memory, and sees a report-generation Java process using 70% of RAM, started by a cron job at the wrong time. She notes its PID, checks its parent with ps -o ppid, and stops it politely with kill (next lesson). The sale pages recover in seconds.

bash
$ ps -eo pid,ppid,user,%mem,cmd --sort=-%mem | head -3
  PID  PPID USER     %MEM CMD
 9812  9790 reports  70.4 java -jar /opt/reports/monthly.jar
 1301     1 apache    0.5 /usr/sbin/httpd -DFOREGROUND

Practice (0/3 done)

Task 1 · Question

Which process has PID 1 on RHEL 9?

Task 2 · Code · Python

ps output lines (after the header) look like "1301 apache 2.3 0.5 httpd": PID, user, %CPU, %MEM, command. Write top_memory(lines, n) that returns the commands of the n processes with the highest %MEM, highest first. lines is a list of strings without the header.

Task 3 · Practice

Start "sleep 1000" in the foreground, pause it, send it to the background, start a second sleep with &, then list jobs and bring the first back to the foreground and stop it with Ctrl+C.

Quick quiz

Question 1

1.In ps output, what does STAT D mean?

Question 2

2.Inside top, which key sorts by memory use?

Question 3

3.A 2-CPU server shows load average 6.0. What does it suggest?

Question 4

4.Which command continues a stopped job in the background?

Question 5

5.How do you get rid of zombie processes?

Read more (free)

Before this, learn

This topic builds on these. If something here feels new, open them first.

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

In ps output, what does STAT D mean?

Answer: Waiting on I/O and cannot be interrupted

D = uninterruptible sleep, usually waiting for a disk.

Inside top, which key sorts by memory use?

Answer: M

M sorts by %MEM; P by %CPU.

A 2-CPU server shows load average 6.0. What does it suggest?

Answer: Work is queueing; the server is overloaded

Load well above the CPU count means processes are waiting.

Which command continues a stopped job in the background?

Answer: bg

bg resumes a stopped job in the background.

How do you get rid of zombie processes?

Answer: Fix or restart the parent process

Zombies are already dead; the parent must collect them.

A 4-core server shows a load average of 11.5, 10.8, 9.9. What does this suggest?Related

Answer: Far more work is waiting than the CPUs can handle

A load well above the number of cores means processes are queuing for CPU (or stuck waiting on I/O).

From Processes, signals and systemd services

Which file shows the kernel arguments of the current boot?Related

Answer: /proc/cmdline

/proc/cmdline.

From The boot process, step by step

What does Ctrl+C send to the foreground process?Related

Answer: SIGINT

Ctrl+C sends SIGINT (2).

From Signals, kill and process priority

Which command shows running containers?Related

Answer: podman ps

podman ps (add -a for stopped ones too).

From Containers with Podman: images, run, ports and logs

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 5 more topics in “Processes, services and logs” before its exam: Signals, kill and process priority, systemd services and systemctl, Targets: the boot goal of the system, Logs: journalctl, rsyslog and a persistent journal, Tuning profiles with tuned, and time with chrony.
  2. Module 3 exam: Processes, services and logs · 14 questions · 25 min, without help. 6 topics of this module still to finish first.
  3. Then the next module, Software and networking. 57 topics left in the course before the final exam.

← Previous: sudo and the sudoers file