A foreground command can be paused and brought back without starting it over.
Ctrl+Z, jobs, and fg
Start sleep 700, and the prompt disappears. Press Ctrl+Z and run jobs -l. Then bring the wait back with fg and interrupt it with Ctrl+C.
First, predict
What happens to sleep 700 after Ctrl+Z?
Now start the exercise, run the command and compare your prediction with the output.
Ctrl+Z sends the foreground command a stop signal. The process freezes in state T, like --blog in the previous lesson, and the shell returns the prompt. The job stays in the table, and you can continue it:
| Command | What it does with the job |
|---|---|
fg |
continues it in the foreground, and the terminal is busy again |
bg |
continues it in the background, and the prompt stays yours |
Ctrl+C interrupts the foreground command, and the process ends. ps marks a foreground command with + in the STAT column: ps itself showed R+ in earlier output.
A long export took over the terminal. You pressed Ctrl+Z and then ran bg. What will jobs show?
-
fgis what takes the terminal again. Afterbgthe job runs in the background and the prompt stays yours. -
bgcontinues a stopped job in the background, andjobsshows Running. Stopped would remain after Ctrl+Z alone. -
bgrestarts nothing: the job stays in the table with the same number and PID.
Ctrl+Z pauses the export, and bg continues the same process in the background. The job is Running again, and the terminal is free.