Scenario
You are multitasking in a terminal while running a long command in the background. You need to list active jobs, move the job into the foreground when you need focus, suspend it, resume it in the background, then terminate it once the work is no longer needed.
Job control is how you stay productive in a shell. It lets you manage long-running tasks without opening extra terminals or losing track of what is running where.
Objective
- Start a long-running command in the background.
- List current jobs and interpret job IDs and states.
- Bring a job to the foreground and suspend it.
- Resume a suspended job in the background.
- Terminate the correct process by PID using
kill.
Concepts
- Job control is scoped to the current shell session and its process group.
-
Background jobs return your prompt but continue running (
&). -
Foreground jobs are attached to the terminal and can be
suspended with Ctrl+Z (
SIGTSTP). -
fgandbgmove a job between terminal-attached and background execution. -
A job number (
%1) is not the same as a PID. Use a PID when sending signals withkill.
Walkthrough
sleep 1000 &
Appending
&
runs the command as a background job and immediately returns
control to your prompt. The shell prints the job number and
the PID it launched.
[1] 1843
jobs
jobs
shows active jobs for the current shell session. Capture the
job spec (%1) and the state (running or stopped).
[1]+ Running sleep 1000 &
fg %1
# OR (only job)
fg
Foregrounding the job attaches it to your terminal. Suspend it with Ctrl+Z to stop the process without killing it, which keeps it tracked by the shell.
sleep 1000
^Z
[1]+ Stopped sleep 1000
bg %1
# OR (only job)
bg
bg
continues a stopped job in the background. Use
jobs
again to confirm it returns to a running state.
[1]+ sleep 1000 &
ps -p 1843
kill 1843
Job numbers and PIDs are different identifiers. Use
ps -p
to confirm the PID is still present, then send
SIGTERM
with
kill
(default behavior) to request clean termination.
[1]+ Terminated sleep 1000
Common breakpoints
jobs
only tracks jobs started in the current shell session. If
the process was started elsewhere, use process tooling
(ps, pgrep) instead of job control.
The job spec is wrong or the job already exited. Re-run
jobs
to confirm the job number, and use the % prefix
(%1) when targeting a specific job.
You signaled a PID without verifying it. Always validate the
PID first with
ps -p 1843, especially when multiple similar processes exist.
Ctrl+Z only affects the foreground process group attached to
your terminal. If the job is still in the background, bring
it to the foreground with
fg
first.
Cleanup checklist
This lab changes runtime state only. Cleanup is confirming the background job is gone and the shell has no remaining tracked jobs.
jobs
ps -p 1843
jobs
returns no active jobs, and
ps -p
shows no such PID.
Reference
-
sleep 1000 &: Runs a command in the background and returns the prompt.-
&: Starts the process as a background job.
-
-
jobs: Lists jobs managed by the current shell session. -
fg %1: Brings a job into the foreground (attaches it to the TTY).-
%1: Refers to job 1 as shown injobsoutput.
-
-
bg %1: Resumes a stopped job in the background. -
ps -p 1843: Checks whether PID 1843 still exists.-
-p: Restricts output to the specified PID.
-
-
kill 1843: SendsSIGTERMby default to request clean termination for PID 1843.