Take control of processes
Suspend, resume and kill jobs, and hunt down a runaway process.
- 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 type | What happens |
|---|---|
command & | Starts it in the background. |
| Ctrl-C | Sends SIGINT, which usually ends the foreground job. |
| Ctrl-Z | Sends SIGTSTP, which pauses it. It isn’t gone. |
jobs | Lists your jobs. |
bg | Resumes a stopped job in the background. |
fg | Brings a job back to the foreground. |
kill %1 | Signals job number 1. |
Try it
Up to 15 XPJuggle your jobs
Press the buttons to run commands and keys, and watch the jobs move between the two panels.
- Start `sleep 300`, and see that it takes over your terminal.
- Pause it without ending it.
- Let it carry on in the background.
- Bring it back to the foreground.
- Stop it for good.
- Start `backup.sh &` straight in the background.
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 PIDsends SIGTERM, a polite request. The program can save its work and clean up. Try this first.kill -9 PIDsends 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 XPStop 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.
- Find the culprit
- 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.
A polite shutdown
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
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…