CodeTheRobot

Ubuntu Linux for Robotics · Lesson 10 of 96

Processes and system monitoring: ps, top, htop and kill

Finding running programs, watching CPU and memory, and stopping a process safely.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain processes and system monitoring: ps, top, htop and kill in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

A process is a running program with a process ID (PID). ps aux lists all processes, top and htop show them live sorted by CPU or memory (press q to leave, F10 in htop). Add & to a command to run it in the background, and jobs and fg to manage it.

kill PID asks a process to end politely (SIGTERM); kill -9 PID forces it and gives the program no chance to clean up, so use it last. pgrep finds a PID by name and pkill kills by name. Watch CPU, memory and temperature on a robot computer before blaming your code.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the processes and system monitoring: ps, top, htop and kill commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

ps aux | head
# List the first processes with owner, CPU and memory.
sleep 300 &
# Start a harmless background process.
pgrep sleep
# Find its PID by name.
htop
# Watch processes live (press F9 to kill, q to quit).
kill $(pgrep sleep)
# Ask it to end (SIGTERM).
kill -9 1234
# Force-kill process 1234 only if it ignores the polite request.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · The robot's computer

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

robot computercpumemdiskdriverstoppedplannerstoppedloggerstoppedsafetystoppedTHE ROBOT'S COMPUTER

Demo for Processes and system monitoring: ps, top, htop and kill. Expected result: running done

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

sleep 300 &
pid=$!
kill -0 "$pid" && echo running
# TODO: ask the process to end with kill, then wait for it
echo done

Expected output

running
done
Stuck? Show the full solution (try it yourself first)
sleep 300 &
pid=$!
kill -0 "$pid" && echo running
kill "$pid"
wait "$pid" 2>/dev/null
echo done

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Start three sleep processes, find them with ps and pgrep, kill one politely and one with kill -9, and note the difference in what each signal does. Use htop to find the process using the most memory.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.