A command can still exist after the shell gives you another prompt. It may be running in the background or it may be stopped. Those states require different next steps. You will move one identified sleep between foreground, stopped and background states, then select the requested one of two jobs.
A finite sleep normally finishes after its requested wait, but a stopped process cannot finish its normal wait until it is continued. Learn the recovery route before the first intentional stop and finish every owned job before leaving this lesson. All work stays in the ordinary guest Bash terminal; there are no files, listeners, administrative commands or shell-setting changes.
1. Distinguish a job, a process and terminal placement
A shell job is Bash's record of a command or pipeline it manages. Here
each job contains one external sleep process, so you can directly match its
job-table PID with the PID captured from $!. More complex jobs can contain
multiple processes; this exercise does not generalize its one-process mapping
to those cases.
| Observation | Meaning |
|---|---|
| Foreground | The job has the terminal; Bash waits before offering another prompt. |
| Background | Bash can offer its prompt while the job remains active. |
| Stopped | Execution is suspended; prompt return is not process completion. |
ps state S | Interruptible sleep; normal for a waiting sleep process. |
ps state R | Running or runnable. |
ps state T | Stopped by job-control/stop activity. |
A jobs-table word such as Running describes an active, non-stopped shell
job. Its process can simultaneously show S in ps. Foreground/background
placement and kernel scheduling state describe different aspects of the same
controlled process.
Bash's jobs -l lists this shell's jobs with their PIDs; -l adds PID detail.
A bracketed job number is local to that shell. A jobspec such as %N means
“job number N in this shell”; it is not an operating-system PID or a reference
that another terminal can safely reuse.
The special jobspec %+ means the current job, a role that can change.
The + marker in fresh jobs output identifies that current entry. We will use
%+ only when the sole current job has just been matched to our captured PID.
Do not store it as though it were a permanent process identity.
2. Start with an empty job table and three absent names
Goal: control only the identified jobs, explain each transition, and finish with no owned job left active or stopped. Use your checked ordinary nonzero Ubuntu guest account and interactive Bash. Run one line at a time and stop after an unexpected result before acting on a selector or PID.
id -u
ps -p $ -o comm=
type -a printf set declare unset jobs fg bg wait kill ps sleep id
Require Bash and the expected nonzero UID. printf, set, declare, unset,
jobs, fg, bg, wait and kill must be Bash builtins; ps, sleep and id
must be expected distribution executables. Require the inspected external
sleep path used by the exercise:
type -P sleep
printf 'Interactive shell flags: %s\n' "$-"
set -o
Require /usr/bin/sleep, the i interactive flag, monitor on, and
allexport/errexit off. monitor is Bash's job-control option. These are
read-only checks; preserve and reconcile any different setting rather than
enabling job control or changing your profile during the task.
Inspect this original shell's job table:
jobs -l
Require no existing entries before starting. Preserve unrelated jobs; do not stop, foreground or clear them to make the example fit. Use a suitable known empty interactive shell and repeat its baseline checks if necessary.
Prove all three local capture names absent before assigning any of them:
declare -p VECTA_LAB_JOB_PID
declare -p VECTA_LAB_JOB_ONE_PID
declare -p VECTA_LAB_JOB_TWO_PID
Each must report absent/not found. Empty, readonly, exported or name-reference declarations are collisions to preserve. Record the actual shell PID and UID using the previous lesson's selected-field format:
printf 'Original Bash PID: %s\n' "$"
id -u
ps -p "$" -o pid,ppid,euid,stat,lstart,args
Only -p selects a PID; -o selects the displayed fields. Record this normal
Bash identity for every later parent/owner comparison.
3. Learn stop recovery before making a stopped job
fg JOBSPEC selects the identified job, continues it if stopped, and gives
it the foreground terminal. bg JOBSPEC continues a stopped job in the
background, returning use of the prompt. They operate on this shell's job
table, which you must freshly inspect before selecting a job.
While a foreground sleep owns the terminal, hold Ctrl and press Z once; no Enter is needed. With the expected terminal behavior, this sends the terminal suspension request (TSTP) to the foreground job. This sleep normally stops and Bash returns a prompt. It is not a request to delete the job. Check jobs and ps before concluding that it stopped.
Have these recovery conditions clear first:
- Stay in the original shell with the actual captured PID and start history.
- At an available prompt, match fresh
jobs -land the selected ps row with that same PID, PPID, EUID, start and command. - If it is the sole verified current stopped job,
bg %+continues it. - Reinspect. A resumed finite sleep may finish promptly if its requested wait has already elapsed; stopped duration alone was not completion.
A narrow signal bridge covers a console that does not deliver Ctrl+Z or an
unavailable job selector. Bash's kill can send signals other than termination:
kill -s SIGNAL -- PID selects a named signal for one positive numeric PID.
STOP cannot be caught, blocked or ignored by the target, unlike terminal
TSTP; CONT continues the stopped process. Sending a signal still requires
the expected permission and must be checked against the observed result/state. Here only a freshly verified captured PID
of one owned sleep is permitted. -- ends options; do not use a negative
number, zero, an unrelated PID or a process-name search as an action target.
If an identified owned process is stopped and bg cannot select its job,
recheck its complete current ps identity from the available original-shell
prompt before kill -s CONT -- "$VECTA_LAB_JOB_PID". That is the bounded
resume fallback for the guided capture. If identity is missing or differs,
do not send a signal to a remembered number; preserve the original terminal
and diagnose the mismatch.
Never type shell commands while the foreground sleep still owns the terminal. If Ctrl+Z is intercepted by the console, let that foreground wait finish and return the original prompt. The separately recorded fresh STOP/CONT practice branch below begins only after that completion, not by signalling the stale PID of the finished foreground attempt.
4. Move one known job through the three states
Read this section before launch so you know the expected keys and checks. Use the already taught asynchronous start and immediate capture, with no other background launch between the two lines:
/usr/bin/sleep 240 &
VECTA_LAB_JOB_PID=$!
Stop on a launch diagnostic before calling it a successful process. Print the actual capture:
printf 'Guided PID: %s\n' "$VECTA_LAB_JOB_PID"
Require a nonempty positive numeric value. Then obtain the two views:
jobs -l
ps -p "$VECTA_LAB_JOB_PID" -o pid,ppid,euid,stat,lstart,args
Require exactly one active job, whose listed PID matches the capture and
whose current + marker selects that same entry. In ps, require the recorded
shell PPID and ordinary EUID, the expected /usr/bin/sleep 240 arguments and
a consistent observed start time. Record its actual job number and complete
process row; do not copy a job number from an example. Truncated or mismatched
evidence is not enough to act on.
Bring that sole checked job to the foreground
Predict: Bash will stop presenting its ordinary prompt while this sleep has the terminal. Immediately after the matching jobs/ps inspection, select the checked sole current job:
fg %+
Bash normally displays the selected command and hands it the foreground.
Confirm it is the intended sleep. While the prompt is absent, press Ctrl+Z
as explained above. If that delivers the expected stop, the original prompt
returns and a stopped-job notification appears. Immediately at that prompt,
record fg's return status before any other command replaces $?, then inspect:
printf 'Foreground return status: %s\n' "$?"
jobs -l
ps -p "$VECTA_LAB_JOB_PID" -o pid,ppid,euid,stat,lstart,args
Require the same PID, owner, parent, start and arguments, with a stopped job
and ps state beginning T. Record the foreground return together with those
state observations; a stop return is not a completed sleep. Prompt return
alone would not establish that state; a naturally completed job also returns
a prompt. If foreground execution finished, its immediately recorded fg
status is the completion result; a later wait need not return that result again. If the process has
already ended, preserve that observation and use the completion/rerun branch
instead of selecting an expired job number.
Continue the same stopped job in the background
With exactly that sole stopped entry still current and freshly matched, continue it:
bg %+
The prompt remains available. Reinspect both views:
jobs -l
ps -p "$VECTA_LAB_JOB_PID" -o pid,ppid,euid,stat,lstart,args
Expect the same live identity in an active, non-stopped job, often S in ps,
or honest natural completion if the finite wait has finished. bg did not
create a new process. If it is still stopped, use the recovery already taught;
do not leave it merely because its requested seconds have passed.

Treat the figure as the expected model to compare with your own jobs and ps evidence. Green control arrows follow the foreground/stop route; the blue continuation arrow returns the same checked job to background placement. These are terminal keys or shell job operations, not new PID assignments. The same captured process is followed through the guided route; the current-job selector must still be rechecked at each use.
If the console intercepts Ctrl+Z
Do not type fallback commands into the still-foreground sleep. Allow it to
finish naturally and return the original Bash prompt. Immediately record
fg's status with the foreground-return printf already shown, before jobs/ps
replaces it. Inspect both views to establish that the old child completed
rather than stopped. Require foreground status 0 and no original active or
stopped job. Do not repeat wait as though this already collected foreground
completion were a new background-child result; a later unknown-child response
would not prove that the recorded foreground execution failed. A changed PID
row means reuse or a mistaken number, not permission to signal it.
Clear only this task's known capture, recheck the empty job table and variable absence, and label the next attempt separately:
unset -v VECTA_LAB_JOB_PID
declare -p VECTA_LAB_JOB_PID
jobs -l
Only with the name absent and table empty, launch a new bounded sleep and capture its new PID:
/usr/bin/sleep 240 &
VECTA_LAB_JOB_PID=$!
Stop on a launch error. Print/check the new positive capture, then require a fresh sole-job mapping and complete expected process identity as in the normal launch. From this available prompt, use the taught signal form:
kill -s STOP -- "$VECTA_LAB_JOB_PID"
jobs -l
ps -p "$VECTA_LAB_JOB_PID" -o pid,ppid,euid,stat,lstart,args
Require that newly recorded process to be stopped. Immediately before resuming, recheck its actual identity again, then continue that same positive PID:
kill -s CONT -- "$VECTA_LAB_JOB_PID"
jobs -l
ps -p "$VECTA_LAB_JOB_PID" -o pid,ppid,euid,stat,lstart,args
Require non-stopped state or normal completion. This branch demonstrates process stop/continue when keyboard delivery is unavailable; record that limitation rather than claiming Ctrl+Z was delivered or that STOP performed a foreground job selection. Finish this child before starting the independent two-job task.
Finish the guided child before adding two jobs
Bash's ordinary wait can return when a job changes state while job control
is enabled. A wait result alone is therefore not the completion check for a
possibly stopped job. First inspect jobs/ps, continue any positively identified
stopped child using the taught recovery, and require it active or already
completed. If its completion was already collected as the successful
foreground fg result, use that recorded status and state evidence and skip
the following wait. For a child continued/running in the background whose
completion still needs collection, use the named wait:
wait "$VECTA_LAB_JOB_PID"
printf 'Guided completion status: %s\n' "$?"
jobs -l
ps -p "$VECTA_LAB_JOB_PID" -o pid,ppid,euid,stat,lstart,args
Require normal sleep status 0, no original live/stopped process, and an
empty job table after any completion notification has been observed. If a
stopped state or unexpected result remains, reconcile it before continuing.
If the numeric PID now belongs to another process, leave that distinct process
alone; do not call its existence a surviving owned sleep.
Only after the guided outcome is reconciled, clear its owned capture with
unset -v VECTA_LAB_JOB_PID. Do not leave it as a stale selector for the next
section.
Diagnose before changing a job's state
| Observation | What to establish |
|---|---|
| Prompt returned. | Did the job stop, finish, or continue in the background? Inspect jobs/ps. |
No such job. | Are you in the original shell, and is that current job mapping still present? |
%+ points somewhere different. | The current-job role changed. Do not assume it still means your earlier PID. |
Running in jobs but S in ps. | Shell activity and scheduling state describe different properties. |
| Wait returned but a job is stopped. | A state-change return is not termination. Verify and continue the owned child. |
| Ctrl+Z did not return the prompt. | Wait for foreground completion; use the separately captured fallback from a real prompt. |
Independent task: select the shorter of two identified jobs
Require an empty original job table and both independent capture names still
absent. Start one /usr/bin/sleep for 180 seconds and one for 120
seconds, capturing each PID immediately in VECTA_LAB_JOB_ONE_PID and
VECTA_LAB_JOB_TWO_PID respectively. Use only the taught launch/capture method.
Before selecting anything, obtain fresh jobs -l output and a complete ps
record for each positive captured PID. Require distinct numeric captures and
two actual live jobs owned by the recorded user and original shell. If a
child already expired, the two-live-job starting condition has not been met:
finish any remaining owned child, record the attempt honestly and rerun from
reconciled empty-table/absent-name conditions. Do not substitute a guessed job.
Your task is to put the 120-second job in the foreground while preserving
the other job. Match its captured PID to its current bracketed job number.
The notation fg %N below is a text template: replace N with that actual
observed number. It is not a command to copy literally, and no fixed %1
is supplied. Use an explicit number for this two-job selection, rather than
assuming the dynamic %+ still represents the desired duration.
Predict which command Bash will display and which job will own the terminal.
If the keyboard stop worked in the guided route, stop the selected foreground
job with Ctrl+Z, inspect its same PID, then continue that same identified
job in the background using the corresponding observed bg %N template.
Re-read the table before choosing its current number; do not act on a row
that has expired or changed.
Whenever fg returns, record its status immediately using the taught printf
before inspecting jobs/ps. Classify the return as a stop or actual completion;
a completed foreground result need not be collected again with wait.
If Ctrl+Z is unavailable, allow the selected foreground job to finish naturally. The earlier STOP/CONT branch supplies the separately labelled stop/resume practice. Do not claim this keyboard sequence occurred or send signals from another terminal to a remembered PID.
Success: selection uses the real PID-to-job-number mapping, the intended 120-second command receives the foreground, and any deliberately stopped child is continued. Explain why its job number cannot identify a job in a different shell. Complete both owned children before cleanup. Attempt this before opening the hints and answer.
Hint 1
Read the duration in the recorded command arguments, then match that process's captured PID to the fresh jobs-table row. Job number and PID serve different roles.
Hint 2
With two jobs, use the current observed numeric jobspec for the chosen row.
Do not select by whichever row happens to carry the current + marker.
Hint 3
The second capture corresponds to duration 120. If its row currently shows
job number N, the foreground template is fg %N, replacing N. After a stop,
use fresh mapping of that same PID before the bg %N template.
Answer and Reasoning
The independent setup uses two immediately captured launches, each only after the corresponding absence check. A valid setup pair is:
/usr/bin/sleep 180 &
VECTA_LAB_JOB_ONE_PID=$!
Stop on a launch diagnostic; print/check the first positive numeric capture and establish its complete identity before proceeding. Then:
/usr/bin/sleep 120 &
VECTA_LAB_JOB_TWO_PID=$!
After the same launch/capture guard, inspect the actual job table and both selected rows:
jobs -l
ps -p "$VECTA_LAB_JOB_ONE_PID" -o pid,ppid,euid,stat,lstart,args
ps -p "$VECTA_LAB_JOB_TWO_PID" -o pid,ppid,euid,stat,lstart,args
Require two live, distinct, correctly owned identities and consistent start
records. The correct row is the one whose PID equals the second capture and
whose command arguments show the expected 120-second wait. Use the observed
number from that row in the text template fg %N. No invented job number
can be a portable solution because the table belongs to your current shell.
When Ctrl+Z is delivered, the same chosen process becomes stopped; the other
job was not selected. After confirming that stopped PID and its current row,
use the corresponding observed bg %N to resume it. If the foreground job
instead finishes naturally, record completion and do not try to resume its
expired job number. Both outcomes preserve which real job was selected;
only the successful key-delivery route establishes the keyboard stop.
Record the immediate fg result and classify it with jobs/ps as taught above.
A naturally completed foreground job already has its completion result.
Continue, wait, inspect, then clear
Check each captured child with jobs/ps. Continue every positively identified
stopped child using its freshly mapped bg %N; the already taught positive-PID
CONT bridge is a fallback only after complete identity verification. Do not
wait indefinitely on a child you have left stopped.
The two captured numbers must differ for separate named completion claims. If they are equal, record the reuse/capture ambiguity instead of claiming two independent wait results; allow/reconcile the finite current launches and redo the two-live-job attempt. No stale-number signal is a repair.
For the successful distinct-capture attempt, first account for any child
whose foreground completion was already collected: use its recorded fg
status and state evidence, and skip that child's wait below. For a remaining
background child, continue it if stopped, then collect its completion with
the corresponding named wait and immediate result:
wait "$VECTA_LAB_JOB_ONE_PID"
printf 'First independent completion status: %s\n' "$?"
Inspect jobs/ps after that result. If it describes a stopped state rather than completion, recover the verified child and finish that check before proceeding. Then handle the second separately:
wait "$VECTA_LAB_JOB_TWO_PID"
printf 'Second independent completion status: %s\n' "$?"
Require normal completion status 0 for each actual sleep and no original
active/stopped process. Observe any jobs completion notification, then require
jobs -l empty. A changed row for a reused numeric PID belongs to a different
identity and must be left alone.
Only after all owned jobs are reconciled, remove the independent variables you actually created:
unset -v VECTA_LAB_JOB_ONE_PID VECTA_LAB_JOB_TWO_PID
The guided variable was cleared after its own completion. If a launch was never performed, clear only captures you actually assigned. No directory, file, setting or other shell job needs cleanup.
Resume with the original shell and record
Keep the original terminal until every deliberately stopped job is continued and finished. A new terminal has a different job table; a copied jobspec from the old shell is not a way to resume the old job there. Reconcile the recorded PID, owner, parent, start, arguments and current state before any bounded resume operation. Missing or changed identity blocks a signal; do not search by program name and act on a convenient match.
A stopped finite sleep is not cleaned up by simply waiting for its nominal duration. If interruption leaves an identified stopped child in the original shell, use the taught fresh mapping and resume path, then complete the named wait/state checks. Begin a new attempt only after the previous owned jobs and captures are accounted for; preserve unrelated jobs and shell configuration.
Sources and other study views
- S18 — Bash: job control, jobspecs, fg/bg, signals and wait
- S19 — GNU Coreutils: bounded sleep
- S25 — Ubuntu Noble ps: PID selection and state evidence
- S73 — Linux signal companion: terminal suspension, STOP and CONT
The expected model uses the declared interactive Bash 5.2 guest and simple GNU sleep jobs. Source descriptions do not replace observing your own terminal key delivery, current job table and process identities.
Revision reconstructs the state and selector decisions; Quick Reference retrieves the guarded operations.