Loading...

Lab 41: Jobs, fg, bg, kill

Start a long-running job in the background, inspect it with jobs, bring it to the foreground, suspend it, then resume it in the background. Terminate the job cleanly with kill after you confirm the correct PID.

core troubleshooting users

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.

Operator context

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).
  • fg and bg move 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 with kill.

Walkthrough

Step 1 : Start a background job.
Command
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
Step 2 : List background and stopped jobs.
Command
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 &
Step 3 : Bring the job to the foreground and suspend it.
Commands
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
Step 4 : Resume the stopped job in the background.
Commands
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 &
Step 5 : Terminate the job by PID after you verify the target.
Commands
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 shows nothing even though something is running

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.

fg/bg returns “no such job”

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.

kill terminates the wrong process

You signaled a PID without verifying it. Always validate the PID first with ps -p 1843 , especially when multiple similar processes exist.

Ctrl+Z does not suspend the job

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.

Commands
jobs
ps -p 1843
Success signal

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 in jobs output.
  • 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 : Sends SIGTERM by default to request clean termination for PID 1843.