The jobs command
The jobs command lists the programs this shell is running in the background or has…
The jobs command lists the programs this shell is running in the background or has paused, each with a job number to use with fg, bg and kill.
Taught in Intermediate, lesson 27
A practice shell in your browser that behaves like bash on Linux for these commands. Nothing you type leaves the page.
Usage
jobs [-l] [-p]
List this shell’s background and paused jobs. A job is a command you started from this shell and sent to the background (with & at the end) or paused (with Ctrl+Z). jobs lists them with their numbers: [1], [2]. The + marks the current job, which fg and bg use when you give no number.
In the practice terminal:Jobs here are pretend programs that run on the practice shell’s clock, which moves a minute each time you press Enter.
How to use jobs
Run a command in the background: &
An & at the end gives you the prompt back at once. The shell prints the job number and the PID.
sleep 600 &[1] 5000
List your jobs
+ marks the current job (the one fg brings back) and - the one before it. -l adds the PIDs.
sleep 600 & sleep 900 & jobs[1] 5000 [2] 5001 [1]- Running sleep 600 & [2]+ Running sleep 900 &
sleep 600 & jobs -l[1] 5000 [1]+ 5000 Running sleep 600 &
Bring a job back: fg
fg brings the current job to the foreground and fg %2 job 2. There, Ctrl+C stops it and Ctrl+Z pauses it again.
sleep 5 & fg[1] 5000 sleep 5
Pause and resume: Ctrl+Z and bg
Ctrl+Z pauses the program in the foreground and makes it a stopped job; bg lets it carry on in the background. Here kill -STOP does what Ctrl+Z would.
sleep 600 & kill -STOP %1; bg %1; jobs[1] 5000 [1]+ sleep 600 & [1]+ Running sleep 600 &
Options
| -l | show process IDs too |
|---|---|
| -p | only the process IDs |
More examples
jobs[1]+ Running sleep 600 &
Questions about jobs
What is the difference between jobs and ps?
jobs lists only what this shell started in the background or paused, by job number. ps lists every process on the machine, by PID.
Do background jobs stop when I close the terminal?
Usually, yes: closing the terminal sends them a hangup signal. Start a long job with nohup (nohup ./backup.sh &) so it keeps running after you log out.
How do I get the prompt back without stopping a program?
Press Ctrl+Z to pause it, then type bg to let it carry on in the background.
Learn it step by step
Related commands
Every everyday command on one page, to print or keep open: the Linux commands cheat sheet.