Skip to content

Jobs and background work

Module
Processes and System Information
Difficulty
Easy
Updated
Tested on
Ubuntu 24.04.5 LTS (lab container), GNU Bash 5.2.21
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.

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.

Earlier lessons briefly used & so a process could remain alive during an inspection. Here is the full relationship:

Terminal window
sleep 300 &
jobs

The & 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.

Terminal window
fg

fg 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:

Terminal window
jobs
Start a background job and bring it forward
learner@rootvolt:~/lab$ sleep 300 &[1] 19learner@rootvolt:~/lab$ jobs[1]+  Running                 sleep 300 &learner@rootvolt:~/lab$ fgsleep 300^Clearner@rootvolt:~/lab$ jobslearner@rootvolt:~/lab$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. An ampersand starts a running background sleep; fg brings it forward, Ctrl+C ends it, and jobs then prints no entries.
Steps (12)
  1. Type sleep 300 &
  2. Press Enter
  3. Output: [1] 19
  4. Type jobs
  5. Press Enter
  6. Output: [1]+ Running sleep 300 &
  7. Type fg
  8. Press Enter
  9. Output: sleep 300
  10. Press Ctrl + C
  11. Type jobs
  12. 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.

Start a separate foreground wait:

Terminal window
sleep 60

While 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.

Terminal window
jobs

Predict 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”
Terminal window
bg
jobs
fg

bg 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:

Terminal window
jobs
Suspend, resume, foreground, and interrupt a job
learner@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$ 

Step-by-step replay of a verified session · Recorded on Ubuntu 24.04.5 LTS, GNU Bash 5.2.21. A real sleep 60 is suspended with Ctrl+Z, resumed with bg, brought forward with fg, and ended with Ctrl+C; the final jobs list is empty.
Steps (19)
  1. Type sleep 60
  2. Press Enter
  3. Press Ctrl + Z
  4. Output: [1]+ Stopped sleep 60
  5. Type jobs
  6. Press Enter
  7. Output: [1]+ Stopped sleep 60
  8. Type bg
  9. Press Enter
  10. Output: [1]+ sleep 60 &
  11. Type jobs
  12. Press Enter
  13. Output: [1]+ Running sleep 60 &
  14. Type fg
  15. Press Enter
  16. Output: sleep 60
  17. Press Ctrl + C
  18. Type jobs
  19. 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.

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.

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.

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.

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; jobs reads this shell’s job table.
  • Ctrl+Z suspends execution; a stopped job still exists.
  • bg resumes in the background, while fg brings 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.