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 using kill by PID once you confirm the correct target.

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 helps you manage long-running tasks without opening extra terminals or losing control of what is running where.

Objective

  • Start a long-running command in the background.
  • List current jobs and read job IDs and states.
  • Bring a job to the foreground and suspend it.
  • Resume a suspended job in the background.
  • Terminate the job by PID using kill.

What You’ll Practice

  • Background execution with &.
  • Job inspection using jobs.
  • Foregrounding and suspending a process with fg and Ctrl+Z.
  • Background resumption with bg.
  • Terminating a job by PID using 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.

[1] 1843
Step 2 : List background and suspended jobs.
Command
jobs

jobs shows active jobs for the current shell. Note the job number (%1) and the state (Running or Stopped).

[1]+  Running                 sleep 1000 &
Step 3 : Bring the job to the foreground and suspend it.
Command
fg %1
# OR (only job)
fg

Foregrounding the job attaches it to your terminal. In this lab flow, you then suspend it with Ctrl+Z, which stops the job but keeps it tracked by the shell.

sleep 1000
^Z
[1]+  Stopped                 sleep 1000
Step 4 : Resume the job in the background.
Command
bg %1
# OR (only job)
bg

bg continues a stopped job in the background. Use jobs again if you want to confirm state.

[1]+ sleep 1000 &
Step 5 : Terminate the background job by PID.
Command
kill 1843
# OR
kill -15 1843

kill sends a signal to the PID. With no signal specified, it sends SIGTERM (15) by default, which is the standard "request to exit" behavior.

[1]+  Terminated              sleep 1000

Reference

  • sleep 1000 & : Starts a long-running command in the background.
  • jobs : Lists jobs managed by the current shell session.
  • fg %<job> : Brings a job into the foreground (attaches it to the TTY).
  • Ctrl+Z : Suspends the foreground job (SIGTSTP), leaving it in a stopped state.
  • bg %<job> : Resumes a stopped job in the background.
  • kill <pid> : Sends SIGTERM by default to request clean termination.
  • kill -15 <pid> : Explicitly sends SIGTERM.