Track 2 · Lesson 2 of 6

Processes and jobs

See what is running with ps and top, stop programs with kill, and manage background jobs in Termux.


Every command you run becomes a process. A process is a running copy of a program, and it gets its own number. When something hangs or drains your battery, you need to find it and stop it. That is what this lesson is about. You will list processes and read what they use. You will stop them with the right signal, and push long tasks into the background.

What you'll be able to do

  • Find which programs are running. See how much memory and CPU each one uses.
  • Stop a process. Choose between SIGTERM and SIGKILL.
  • Run a command in the background, then bring it back.
  • List and manage jobs with jobs, fg and bg.
  • See why a password typed on the command line is visible to others.

Step 1: What a process is

When you run a command, the kernel starts a process. Every process has a process ID, or PID, and a parent. The shell you type into is a process too. The ps command lists the processes attached to your terminal.

ps
  PID TTY          TIME CMD
 5234 pts/0    00:00:00 bash
 5487 pts/0    00:00:00 ps

The shell is PID 5234. The ps you ran is 5487. It disappears the moment it finishes.

Step 2: List everything and watch it with top

ps aux shows every process on the device, not only yours. It adds columns for the user, CPU and memory.

ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
u0_a123   5234  0.0  0.4  22144  4096 pts/0    S    10:01   0:00 bash
u0_a123   5487  0.0  0.1  10240  2048 pts/0    R    10:05   0:00 ps aux

The %CPU and %MEM columns show the share of the processor and memory a process uses. The STAT column shows its state. R means running, and S means sleeping, waiting for something.

For a live view, use top. It refreshes on its own. Press q to leave.

top
Tasks: 2 total,   1 running,   1 sleeping
Mem:   3.7G total,  2.1G used,  1.6G free
PID USER      PR  NI    VIRT    RES  %CPU  %MEM     TIME+ COMMAND
5234 u0_a123  20   0   22144   4096   0.3   0.1   0:00.12 bash
5487 u0_a123  20   0   10240   2048   0.0   0.1   0:00.00 top
Sort the list

Inside top, press M to sort by memory and P to sort by CPU. Press q to leave.

Step 3: Stop a process with kill

The kill command sends a signal to a process by PID. The default is SIGTERM. It asks the program to shut down, and lets it save its work first.

kill 5234

The process is gone once it handles the signal.

ps -p 5234
  PID TTY          TIME CMD

Only the header prints. No process has that PID now.

Sometimes a program ignores SIGTERM. Suppose PID 6100 will not stop. SIGKILL ends it at once. It cannot be caught, so the program gets no chance to clean up.

kill -KILL 6100
Try SIGTERM first

Reach for SIGKILL, also written kill -9, only when SIGTERM fails. A killed program cannot close files or flush data, so you can lose work.

Step 4: Background jobs

A long command holds your terminal until it finishes. Add & to send it to the background. You get your prompt back at once. The shell prints the job number and the PID.

sleep 300 &
[1] 6001

The jobs command lists what is running in the background of this shell.

jobs
[1]+  Running                 sleep 300 &

To bring a job back to the foreground, use fg. It takes over the terminal until you stop it.

fg
sleep 300

Press Ctrl-C to end it. To keep a foreground job running, press Ctrl-Z to pause it. Then send it back with bg.

sleep 300
^Z
[1]+  Stopped                 sleep 300
bg
[1]+ sleep 300 &
jobs
[1]+  Running                 sleep 300 &

Step 5: Why command-line secrets are visible

Any user on the device can read every command line through ps aux. If you pass a password as an option, it stays on show while the program runs.

sshpass -p hunter2 ssh user@example.com

While that runs, another session sees the whole line, password included.

ps aux | grep sshpass
u0_a123   6110  0.0  0.2  12345  2048 pts/0    S    10:10   0:00 sshpass -p hunter2 ssh user@example.com

That is a real leak. Prefer a tool that asks for the password at a prompt. It can also read it from a file only you can open. Or take it from an environment variable.

Quick reference

CommandWhat it does
psLists processes on your terminal
ps auxLists every process with user, CPU and memory
topShows a live, refreshing list of processes
kill PIDSends SIGTERM to ask a process to stop
kill -KILL PIDEnds a process at once, with no cleanup
command &Runs a command in the background
jobsLists background jobs in this shell
fgBrings a job to the foreground
bgResumes a stopped job in the background

Next steps

Test yourself on this

4 questions and 1 task where you run a real command, with the reasoning explained either way.

Start