Lesson 2: The Juggler (Background & Foreground)
A DevOps engineer doesn't just run one command at a time — they juggle multiple processes like a circus performer. Understanding foreground and background processes is the key to multitasking in the terminal.
Foreground vs. Background
- Foreground: The command takes over your terminal. You can't type anything else until it finishes.
- Background: The command runs silently while you continue working.
The Magic Ampersand: &
Add & at the end of any command to run it in the background:
sleep 300 &
This starts a 5-minute sleep timer without blocking your terminal.
Job Control Commands
jobs— List all background jobs in the current session.fg %1— Bring job #1 back to the foreground.bg %1— Resume a paused job in the background.Ctrl + Z— Pause (suspend) the current foreground process.
The nohup Command
When you close your terminal, all your background jobs die. To keep a process running even after logout:
nohup ./my-script.sh &
nohup = "No Hang Up". Essential for long-running deployments.
booting...
Mission Objective
You need to run a long monitoring task while continuing to work:
- Start juggling: Run
sleep 300 &to create a background process. - Check your act: Run
jobsto see it running in the background. - Take control: Bring it back to the foreground with
fg %1.