jobs command in Linux CLI - Time & Space Complexity
We want to understand how the time to run the jobs command changes as the number of background tasks grows.
How does the command's work increase when there are more jobs to list?
Analyze the time complexity of the following code snippet.
jobs
This command lists all current background jobs in the shell session.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The shell checks each background job to gather its status and details.
- How many times: Once for each job currently running or stopped in the background.
As the number of background jobs increases, the command takes longer because it must check each job.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Checks 10 jobs |
| 100 | Checks 100 jobs |
| 1000 | Checks 1000 jobs |
Pattern observation: The work grows directly with the number of jobs; doubling jobs doubles the work.
Time Complexity: O(n)
This means the time to run jobs grows in a straight line with the number of background jobs.
[X] Wrong: "The jobs command always runs instantly, no matter how many jobs there are."
[OK] Correct: The command must check each job, so more jobs mean more work and longer time.
Understanding how commands scale with input size helps you think clearly about performance in real tasks.
"What if the shell cached job statuses instead of checking each time? How would the time complexity change?"
Practice
jobs command do in a Linux shell?Solution
Step 1: Understand the purpose of
Thejobsjobscommand 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 tojobs.Final Answer:
Lists background and stopped tasks in the current shell -> Option AQuick Check:
jobslists current shell jobs [OK]
- Confusing jobs with ps command
- Thinking jobs starts or kills tasks
- Assuming jobs shows system-wide processes
jobs command?Solution
Step 1: Recall
Thejobsoptions-loption withjobslists jobs with their process IDs.Step 2: Verify other options
-plists only PIDs without job info,--list-pidis invalid, and-ashows all jobs including those not started by the shell.Final Answer:
jobs -l -> Option DQuick Check:
jobs -lshows jobs with PIDs [OK]
- Using invalid options like --list-pid
- Confusing -p with -l
- Assuming -a shows PIDs
$ sleep 100 & [1] 12345 $ sleep 200 & [2] 12346 $ jobs [1]+ Running sleep 100 & [2]- Running sleep 200 &What will be the output of
jobs -l?Solution
Step 1: Understand jobs output
Thejobscommand shows job number, status, and command. The-loption adds the process ID after the job number.Step 2: Match output format
Since both jobs are running with PIDs 12345 and 12346,jobs -lwill show job number, PID, status, and command.Final Answer:
[1]+ 12345 Running sleep 100 & [2]- 12346 Running sleep 200 & -> Option CQuick Check:
jobs -ladds PIDs to normal jobs output [OK]
- Ignoring PIDs in output
- Confusing Running with Stopped status
- Expecting no jobs listed
sleep 300 & and then jobs shows the job as stopped. Which command will resume the job in the background?Solution
Step 1: Understand job states and commands
A stopped job is paused. Thebgcommand resumes it in the background, whilefgresumes it in the foreground.Step 2: Choose correct command for background resume
Since the question asks to resume in background,bg %1is correct.killterminates, andstopis invalid here.Final Answer:
bg %1 -> Option BQuick Check:
Resume stopped job in background = bg %job_number [OK]
- Using fg instead of bg for background resume
- Trying to kill instead of resume
- Using invalid stop command
Solution
Step 1: Bring job 3 to foreground
Usefg %3to bring job number 3 to the foreground so you can interact with it.Step 2: Stop the job temporarily
PressingCtrl+Zsends a stop signal, pausing the job and returning control to the shell.Final Answer:
fg %3, then press Ctrl+Z -> Option AQuick Check:
Foreground job then Ctrl+Z to stop it [OK]
- Using bg instead of fg to bring job forward
- Trying to kill instead of stop
- Using invalid stop command
Ready to prove it?
Pass the test and this pattern moves unknown
