Using the jobs Command to Manage Background Processes
📖 Scenario: You are working in a Linux terminal and want to manage multiple tasks running in the background. You need to see which tasks are running, stopped, or in the background.
🎯 Goal: Learn how to use the jobs command to list background and stopped jobs in the current shell session.
📋 What You'll Learn
Start a background job using &
Use the jobs command to list jobs
Understand job states like Running and Stopped
💡 Why This Matters
🌍 Real World
Managing multiple tasks in the terminal without closing the shell is common for developers and system administrators.
💼 Career
Knowing how to control background jobs helps in multitasking and efficient use of the command line.
Progress0 / 4 steps
1
Start a Background Job
Run the command sleep 30 & to start a background job that waits for 30 seconds.
Linux CLI
Hint
Use & at the end of the command to run it in the background.
2
Start Another Background Job
Run the command sleep 60 & to start another background job that waits for 60 seconds.
Linux CLI
Hint
Use & to run the second sleep command in the background as well.
3
List All Background Jobs
Use the jobs command to list all background jobs and their statuses.
Linux CLI
Hint
Simply type jobs and press Enter to see the list of jobs.
4
Display the Output of the jobs Command
Run the script and observe the output of the jobs command showing the background jobs.
Linux CLI
Hint
The output should list two jobs with their job numbers and status as Running.
Practice
(1/5)
1. What does the jobs command do in a Linux shell?
easy
A. Lists background and stopped tasks in the current shell
B. Shows all running processes on the system
C. Terminates all background jobs
D. Starts a new background job
Solution
Step 1: Understand the purpose of jobs
The jobs command shows tasks started in the current shell that are either stopped or running in the background.
Step 2: Compare with other options
Options A, B, and C describe different commands or actions unrelated to jobs.
Final Answer:
Lists background and stopped tasks in the current shell -> Option A
Quick Check:
jobs lists current shell jobs [OK]
Hint: Remember: jobs shows your shell's paused or background tasks [OK]
Common Mistakes:
Confusing jobs with ps command
Thinking jobs starts or kills tasks
Assuming jobs shows system-wide processes
2. Which of the following is the correct syntax to list all jobs with their process IDs using the jobs command?
easy
A. jobs -a
B. jobs --list-pid
C. jobs -p
D. jobs -l
Solution
Step 1: Recall jobs options
The -l option with jobs lists jobs with their process IDs.
Step 2: Verify other options
-p lists only PIDs without job info, --list-pid is invalid, and -a shows all jobs including those not started by the shell.
Final Answer:
jobs -l -> Option D
Quick Check:
jobs -l shows jobs with PIDs [OK]
Hint: Use -l to see job numbers plus process IDs [OK]
4. You ran sleep 300 & and then jobs shows the job as stopped. Which command will resume the job in the background?
medium
A. kill %1
B. bg %1
C. fg %1
D. stop %1
Solution
Step 1: Understand job states and commands
A stopped job is paused. The bg command resumes it in the background, while fg resumes it in the foreground.
Step 2: Choose correct command for background resume
Since the question asks to resume in background, bg %1 is correct. kill terminates, and stop is invalid here.
Final Answer:
bg %1 -> Option B
Quick Check:
Resume stopped job in background = bg %job_number [OK]
Hint: Use bg %job_number to resume stopped jobs in background [OK]
Common Mistakes:
Using fg instead of bg for background resume
Trying to kill instead of resume
Using invalid stop command
5. You have multiple background jobs running. You want to bring job number 3 to the foreground and then stop it temporarily. Which sequence of commands should you use?
hard
A. fg %3, then press Ctrl+Z
B. bg %3, then press Ctrl+C
C. kill %3, then fg %3
D. jobs %3, then stop %3
Solution
Step 1: Bring job 3 to foreground
Use fg %3 to bring job number 3 to the foreground so you can interact with it.
Step 2: Stop the job temporarily
Pressing Ctrl+Z sends a stop signal, pausing the job and returning control to the shell.
Final Answer:
fg %3, then press Ctrl+Z -> Option A
Quick Check:
Foreground job then Ctrl+Z to stop it [OK]
Hint: Use fg %job then Ctrl+Z to pause foreground job [OK]
Common Mistakes:
Using bg instead of fg to bring job forward
Trying to kill instead of stop
Using invalid stop command
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.