LearnAI ToolsCareerPractice BuildsPlayDevelopmentContact
Lesson 913 min read

Processes and Jobs: ps, top, kill and Background Tasks

See what is running with ps and top, run commands in the background, and stop programs with Ctrl+C, kill and pkill.

On this page 11 sections

Introduction

A web server that will not stop, a script stuck in a loop, a program using all the memory — sooner or later you need to see what is running and take control of it. This lesson covers processes: how to list them, run them in the background and stop them.

What You Will Learn
  • What a process and a process ID (PID) are.
  • How to list processes with ps and watch them live with top.
  • How to run commands in the background and manage them as jobs.
  • How to stop programs with Ctrl+C, kill and pkill.

What Is a Process?

A process is a running copy of a program. Running ls creates a process that lives for a split second; a web server is a process that runs for months. Every process has a unique number, its PID (process ID), which is how you refer to it when you want to stop it.

Listing Processes: ps

ps on its own shows the processes started from your current terminal — usually just your shell (bash) and ps itself:

ps
Output

Click Run to see what this code prints.

ps aux lists every process on the system, with its owner, CPU and memory use. On a normal computer that is hundreds of lines, so you usually filter it with grep:

ColumnMeaning
USERWho started the process
PIDIts process ID
%CPU / %MEMShare of processor and memory it is using
STATState: R running, S sleeping (waiting), Z zombie (finished but not cleaned up)
START / TIMEWhen it started / how much CPU time it has used
COMMANDThe command that started it

Background Jobs

Normally a command occupies your terminal until it finishes. Adding & runs it in the background so you can keep working. sleep simply waits for a number of seconds, which makes it a harmless way to practise. jobs lists what you started in the background from this terminal:

sleep 600 &
sleep 900 &
jobs
Output

Click Run to see what this code prints.

[1] and [2] are job numbers, local to your terminal. The + marks the job that fg (foreground) would bring back. Now the processes show up in the system list:

ps aux | grep -E "^USER|sleep [0-9]"
Output

Click Run to see what this code prints.

Command or keyWhat it does
command &Start a command in the background
jobsList background jobs from this terminal
fg %1Bring job 1 back to the foreground
Ctrl+ZPause the foreground program
bg %1Let a paused job continue in the background
Ctrl+CStop the foreground program

Finding and Stopping Processes

pgrep finds processes by name and prints their PIDs (-a adds the full command):

pgrep -a sleep
Output

Click Run to see what this code prints.

kill <PID> asks a process to stop. Despite the name, it sends a polite request (the TERM signal), which lets the program save its work and exit cleanly. Here Priya stops the older of the two:

kill $(pgrep -o -x sleep)
pgrep -a sleep
Output

Click Run to see what this code prints.

($(...) runs the command inside and uses its output — here the PID of the oldest sleep process.) pkill stops processes by name, so every matching process goes at once:

pkill sleep
pgrep -a sleep || echo "no sleep processes left"
Output

Click Run to see what this code prints.

If a frozen program ignores the polite request, kill -9 sends the KILL signal, which ends it immediately with no chance to clean up:

sleep 500 &
PID=$!
echo "started sleep with PID $PID"
kill -9 $PID
Output

Click Run to see what this code prints.

$! holds the PID of the last background command — very handy in scripts.

Mini Challenge · Fix the Command

Your development server (PID 4120) is not responding to Ctrl+C. What should you try first?

Choose one answer

Live View: top and htop

top shows a live, constantly updating table of processes, sorted by CPU use — the command-line equivalent of Task Manager. Press q to quit. Its header summarises the whole system:

top -b -n 1 | head -n 5
Output

Click Run to see what this code prints.

(-b -n 1 prints a single snapshot instead of the interactive view.) The header shows uptime, load averages for the last 1, 5 and 15 minutes, task counts, CPU usage and memory. The recording above comes from a small container, which is why it lists only a few tasks — your own computer will show hundreds. Many people prefer htop, a friendlier, colourful version: install it with sudo apt install htop.

Keeping Jobs Running: nohup

Background jobs normally stop when you close the terminal or disconnect from a server. nohup (no hang-up) keeps a command running anyway and saves its output to a file:

nohup bash -c "sleep 1; echo backup finished" > backup.out 2>&1 &
sleep 2
cat backup.out
Output

Click Run to see what this code prints.

For anything that should run permanently, such as a web server, use a proper service manager instead (systemd, lesson 14) — it also restarts the program if it crashes.

Mini Challenge · Put It in Order

A program is slowing your server down. Put the steps to deal with it in order.

  1. Ask it to stop with kill <PID>
  2. Only if it is still running, use kill -9 <PID>
  3. Check with ps or pgrep that it has gone
  4. Look at the busiest processes with top
  5. Note the PID of the problem process

Common Mistakes

Avoid These Mistakes
  • Reaching for kill -9 first, which can leave corrupted files or half-written data.
  • Using pkill with a short, vague name and stopping more processes than intended — check with pgrep -a first.
  • Starting a long job on a server without nohup (or a service), then losing it when the connection drops.
  • Confusing job numbers (%1) with PIDs (4120).

Frequently Asked Questions

A process that has finished but whose parent has not yet collected its exit status. It uses no CPU or memory and usually disappears on its own; many zombies indicate a bug in the parent program.

The first process started at boot — on most modern distributions it is systemd. It starts and supervises everything else and must never be killed.

A terminal multiplexer: it keeps whole terminal sessions running on a server after you disconnect, and lets you split the screen. It is a popular alternative to nohup for interactive work.

Key Takeaways

  • A process is a running program, identified by its PID.
  • ps lists processes, ps aux lists all of them, top shows them live.
  • & runs a command in the background; jobs, fg and bg manage those jobs.
  • kill sends a polite stop request; kill -9 forces it; pkill stops by name.
  • nohup keeps a command running after you disconnect.

Summary

You can now see and control everything running on a Linux system. Next you will install new software — the Linux way, with a package manager.

Next Lesson →

Installing Software with apt