Loading
0x150Lesson 22 of 28

Take control of processes

Suspend, resume and kill jobs, and hunt down a runaway process.

22 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • Move jobs between the foreground and background
  • Stop a job with Ctrl-C, Ctrl-Z and kill, and explain the difference
  • Find processes with ps, and choose the right signal

A long command is holding your terminal, and you need your prompt back without losing the work. The shell can juggle several jobs at once, as long as you know how to move them around.

Foreground, background and stopped

You typeWhat happens
command &Starts it in the background.
Ctrl-CSends SIGINT, which usually ends the foreground job.
Ctrl-ZSends SIGTSTP, which pauses it. It isn’t gone.
jobsLists your jobs.
bgResumes a stopped job in the background.
fgBrings a job back to the foreground.
kill %1Signals job number 1.

Try it

Up to 15 XP

Juggle your jobs

Press the buttons to run commands and keys, and watch the jobs move between the two panels.

  1. Start `sleep 300`, and see that it takes over your terminal.
  2. Pause it without ending it.
  3. Let it carry on in the background.
  4. Bring it back to the foreground.
  5. Stop it for good.
  6. Start `backup.sh &` straight in the background.
Foreground$ _
BackgroundNo jobs.

Your prompt is free. Start something.

Finding and signaling processes

ps aux lists every process, and top or htop shows them live. To end one, kill sends a signal to its PID:

  • kill PID sends SIGTERM, a polite request. The program can save its work and clean up. Try this first.
  • kill -9 PID sends SIGKILL, which can’t be caught. The process just stops, with no cleanup, so it can leave corrupted files or stale locks. Use it last.

A process that survives a closed terminal needs nohup command &, or better, tmux, which the next lesson covers.

Try it

Up to 15 XP

Stop the runaway

The station’s server has slowed to a crawl. ps shows you what’s running, with its CPU and memory use.

Mission 1 of 2

Find the culprit

Hopper: “Something’s eating the CPU. Which PID?”

Simulated shell. Try ls, or type help to see the commands.

Up arrow: history. Tab: complete. Ctrl-L: clear. Ctrl-C: cancel.
  1. Find the culprit
  2. Stop it, and nothing else

Key takeaways

  • Ctrl-C ends a job, and Ctrl-Z only pauses it.

  • bg and fg move jobs around.

  • Try kill first, and use kill -9 last.

Lesson quiz

5 questions · pass with 4 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write Bash scripts

Write a script in the editor and run it for real against sample input. Each run gets a fresh Linux sandbox with Bash 5.2 and the GNU tools on Wandbox, a free public service - so experiment freely, even with rm. Your script and test input are sent there.

Exercise 1

A polite shutdown

+25 XP

The starter starts a background job that handles SIGTERM. Send it SIGTERM with kill, wait for it to finish, then print job exited with status N, using the job’s exit status.

  • Lets the job clean up
script.sh
Loading editor…

Your script runs with Bash 5.2 and GNU tools on Wandbox, a free public service, in a fresh sandbox each time. Your script and test input are sent to that service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…