Jobs and background work
On this page
A waiting command owns the terminal, but you want the prompt back without ending that command. Pressing Ctrl+C would end this wait. You need a way to pause it or let it continue while Bash accepts another command.
By the end, you can distinguish foreground, running background, and stopped jobs, then move your own waiting command between those states and finish it deliberately.
Before you start
Section titled “Before you start”Finish Signals and stopping processes, including its reset. Use an interactive Bash terminal and the B04 lab directory. Job control belongs to that shell session, so keep the same terminal open throughout each sequence.
This lesson creates no files. Each sequence ends by interrupting its own foreground sleep and checking that jobs is empty. Your PIDs, job numbers, timing, and job-notice spacing may differ. The recordings use a lab container with few processes and a Python capture program as PID 1, not systemd. Its small process list does not represent a whole Ubuntu desktop.
Give the prompt back while work continues
Section titled “Give the prompt back while work continues”Earlier lessons briefly used & so a process could remain alive during an inspection. Here is the full relationship:
sleep 300 &jobsThe & terminates an asynchronous command: Bash starts it without waiting for completion, then returns the prompt. The command is a background job of this shell. A job is Bash’s unit for tracking a command or pipeline; a pipeline can contain several processes. Our single-command jobs each contain one sleep process.
Bash’s launch line in this recording contains [1] followed by PID 19. The brackets identify job 1 within this shell. The unbracketed number is a process ID. They are not interchangeable identifiers, even when their numeric values happen to coincide elsewhere.
jobs lists jobs known to this shell, not all of your processes across the machine. Its Running label means that the job is continuing rather than suspended. The sleep process can still be waiting in process state S from B13.2. A running job need not be consuming CPU continuously.
The + marks the current job, the default selected by fg and bg. If several jobs exist, - marks the previous job. These markers are shell choices, not parts of a PID.
Bring background work to the terminal
Section titled “Bring background work to the terminal”fgfg brings the current job to the foreground, where it owns terminal input and Bash waits for it. It prints the command being resumed or brought forward. In this example there is one job, so the default is unambiguous. When selecting explicitly, fg %1 means job 1; the % distinguishes a job specification from a bare process ID.
While sleep is in the foreground, press Ctrl+C, then run:
jobslearner@rootvolt:~/lab$ sleep 300 &[1] 19learner@rootvolt:~/lab$ jobs[1]+ Running sleep 300 &learner@rootvolt:~/lab$ fgsleep 300^Clearner@rootvolt:~/lab$ jobslearner@rootvolt:~/lab$ Steps (12)
- Type
sleep 300 & - Press Enter
- Output:
[1] 19 - Type
jobs - Press Enter
- Output:
[1]+ Running sleep 300 & - Type
fg - Press Enter
- Output:
sleep 300 - Press Ctrl + C
- Type
jobs - Press Enter
The first jobs listing says Running. After fg, the shell cannot accept another command until the foreground job stops or ends. Ctrl+C ends this sleep; the final empty jobs listing establishes that this shell has no remaining job from the sequence.
Suspend without terminating
Section titled “Suspend without terminating”Start a separate foreground wait:
sleep 60While it is waiting, press Ctrl+Z. The terminal sends the suspend signal, TSTP, to foreground work. Sleep stops, Bash reports Stopped, and the prompt returns. The job still exists; it has not completed or cleaned up resources.
jobsPredict first: with this job labeled Stopped, does the 60-second wait continue executing in the background?
Run it, then compare with your guess
No. A stopped process is suspended. Returning the prompt alone does not prove that it is running in the background. The job remains stopped until it is resumed or terminated. Wall-clock time still passes while execution is stopped; on resumption, a timed operation may discover its deadline has already passed.
This is why Ctrl+Z and Ctrl+C solve different problems. Ctrl+Z suspends this command for later continuation. Ctrl+C sends INT, whose default action ends sleep. Neither key means “remove this program from disk.”
Resume in the background, then return to the foreground
Section titled “Resume in the background, then return to the foreground”bgjobsfgbg resumes the current stopped job in the background, using a continue signal. It does not launch a replacement process. The job number and process identity stay with the existing instance. The next jobs listing says Running rather than Stopped.
fg brings that same job to the foreground. It can also resume a stopped job directly, so bg is not a mandatory step before every fg. This sequence uses both commands to show their different destinations.
Press Ctrl+C once sleep is in the foreground, then check again:
jobslearner@rootvolt:~/lab$ sleep 60^Z[1]+ Stopped sleep 60learner@rootvolt:~/lab$ jobs[1]+ Stopped sleep 60learner@rootvolt:~/lab$ bg[1]+ sleep 60 &learner@rootvolt:~/lab$ jobs[1]+ Running sleep 60 &learner@rootvolt:~/lab$ fgsleep 60^Clearner@rootvolt:~/lab$ jobslearner@rootvolt:~/lab$ Steps (19)
- Type
sleep 60 - Press Enter
- Press Ctrl + Z
- Output:
[1]+ Stopped sleep 60 - Type
jobs - Press Enter
- Output:
[1]+ Stopped sleep 60 - Type
bg - Press Enter
- Output:
[1]+ sleep 60 & - Type
jobs - Press Enter
- Output:
[1]+ Running sleep 60 & - Type
fg - Press Enter
- Output:
sleep 60 - Press Ctrl + C
- Type
jobs - Press Enter
In this capture, ^Z appears first, followed immediately by Bash’s [1]+ Stopped notice and a returned prompt. The explicit jobs command reports Stopped too. Bg prints [1]+ sleep 60 &; the following jobs command reports Running. Fg prints sleep 60, and Ctrl+C produces ^C. No Done line appears after the interrupt in this recording; jobs simply prints nothing.
Bash chooses when to print job notices, often just before a prompt. Do not expect every asynchronous completion notice to appear at the instant a process ends. The replay preserves the order Bash printed them in.
Keep terminal input and output in mind
Section titled “Keep terminal input and output in mind”Background execution does not detach a job from its terminal or turn it into a service. Output can still appear among your prompts. A background command that tries to read terminal input is normally stopped by job control. Sleep needs no input and produces no ordinary output, which makes it suitable for this bounded exercise.
Use fg before interacting with a job that needs terminal input. Do not assume closing the terminal preserves background work. Durable sessions and service management belong to later modules; & alone makes no persistence guarantee.
Mistakes to recognize
Section titled “Mistakes to recognize”Calling Stopped a successful finish. It means suspended execution. Use bg or fg to continue, then finish your practice command.
Using a job number in a PID command. %1 identifies a shell job; the launch line’s unbracketed number identifies a process. Recheck the current jobs and process listing before choosing a target.
Running jobs in another terminal. A different Bash session has a different job table. An empty table there says nothing about the original shell’s jobs.
Try it yourself
Section titled “Try it yourself”Goal: A job is Stopped and you need to type input into it. Choose between bg and fg, and explain whether you must restart the program. Use the recorded sequence to justify your choice without leaving another process running.
Solution
Choose fg to resume the existing job in the foreground, where it can use terminal input. You do not restart it. Bg would resume it in the background, where terminal reads can stop it again. The captured fg brings forward the same job that bg resumed.
Reset this lesson
Section titled “Reset this lesson”Both recorded sequences finish with foreground Ctrl+C and an empty jobs listing. No files or variables were created. If you leave the second sequence at Stopped or Running, use jobs to identify your practice sleep, bring it forward with fg, interrupt it, and check jobs again. Do not leave a suspended job behind merely because the prompt is available.
&starts background work and returns the prompt;jobsreads this shell’s job table.- Ctrl+Z suspends execution; a stopped job still exists.
bgresumes in the background, whilefgbrings work to the foreground and can resume it there.- Ctrl+C ends the foreground sleep in these examples; verify the final empty job table.
Next, Practice: Processes and System Information combines process, resource, job-control, and cleanup evidence.
Further reading
Section titled “Further reading”- In Bash:
help jobs,help bg, andhelp fg. - GNU Bash: Job control basics and job control builtins.
- Ubuntu Bash manual, under JOB CONTROL.