Bird
Raised Fist0
Linux CLIscripting~20 mins

Background processes (&) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Background Process Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this command sequence?
Consider the following commands run in a Linux shell:

sleep 2 & echo Done

What will be printed immediately after running these commands?
Linux CLI
sleep 2 & echo Done
ADone after 2 seconds
BDone
CNo output until sleep finishes
Dsleep: command not found
Attempts:
2 left
💡 Hint
The ampersand (&) runs the first command in the background, so the shell does not wait for it.
💻 Command Output
intermediate
2:00remaining
What happens when you run multiple background jobs?
You run these commands in a Linux shell:

sleep 3 & sleep 1 & sleep 2 & wait

What will happen after running these commands?
Linux CLI
sleep 3 & sleep 1 & sleep 2 & wait
AThe shell waits until all three sleep commands finish before returning to prompt
BThe shell returns immediately without waiting
COnly the first sleep command runs in background, others run in foreground
DThe shell waits only for the first sleep command
Attempts:
2 left
💡 Hint
The 'wait' command waits for all background jobs to finish.
🔧 Debug
advanced
1:30remaining
Why does this background command fail to run properly?
You try to run this command in a shell:

echo Hello & & echo World

What error will the shell produce?
Linux CLI
echo Hello & & echo World
Abash: syntax error near unexpected token `&'
BHello\nWorld
CNo output, command hangs
DHello
Attempts:
2 left
💡 Hint
Check how many ampersands are used and their placement.
🚀 Application
advanced
2:00remaining
How to run a script in background and save its output to a file?
You want to run a script named backup.sh in the background and save both its output and errors to backup.log. Which command does this correctly?
A./backup.sh >& backup.log &
B./backup.sh & > backup.log 2>&1
C./backup.sh &> backup.log 2>&1
D./backup.sh > backup.log 2>&1 &
Attempts:
2 left
💡 Hint
Redirect output before putting command in background.
🧠 Conceptual
expert
2:00remaining
What is the effect of running a command with nohup and & together?
You run this command:

nohup longtask.sh &

What happens to the process if you close the terminal?
AThe process stops immediately when terminal closes
BThe process pauses until terminal is reopened
CThe process keeps running even after terminal closes
DThe process restarts automatically after terminal closes
Attempts:
2 left
💡 Hint
nohup prevents hangup signals from stopping the process.

Practice

(1/5)
1. What does adding & at the end of a command in Linux do?
easy
A. Runs the command with higher priority
B. Stops the command from running
C. Runs the command in the background, freeing the terminal immediately
D. Saves the command output to a file

Solution

  1. Step 1: Understand the role of & in Linux commands

    Adding & at the end of a command tells the shell to run it in the background.
  2. Step 2: Effect on terminal usability

    Running a command in the background frees the terminal immediately for other commands.
  3. Final Answer:

    Runs the command in the background, freeing the terminal immediately -> Option C
  4. Quick Check:

    Background process = command + & [OK]
Hint: Remember: & means run in background [OK]
Common Mistakes:
  • Thinking & stops the command
  • Confusing & with output redirection
  • Assuming & increases priority
2. Which of the following is the correct syntax to run sleep 10 in the background?
easy
A. sleep & 10
B. & sleep 10
C. sleep 10 &&
D. sleep 10 &

Solution

  1. Step 1: Identify correct placement of &

    The & must be at the end of the command to run it in the background.
  2. Step 2: Check each option

    sleep 10 & places & correctly after sleep 10. Others place it incorrectly or use wrong syntax.
  3. Final Answer:

    sleep 10 & -> Option D
  4. Quick Check:

    Command + & at end = background run [OK]
Hint: Put & at the end of the command [OK]
Common Mistakes:
  • Placing & before the command
  • Using && instead of &
  • Splitting & from command arguments
3. What will be the output after running these commands?
sleep 5 &
jobs
medium
A. Immediately exits the terminal
B. Lists the sleep job running in the background
C. Displays an error about sleep command
D. Shows no jobs running

Solution

  1. Step 1: Run sleep 5 &

    This starts the sleep command in the background for 5 seconds.
  2. Step 2: Run jobs

    This command lists all background jobs currently running, including the sleep job.
  3. Final Answer:

    Lists the sleep job running in the background -> Option B
  4. Quick Check:

    jobs shows background tasks [OK]
Hint: Use jobs to see background tasks [OK]
Common Mistakes:
  • Expecting jobs to show nothing
  • Confusing jobs with ps command
  • Thinking sleep runs in foreground without &
4. You ran sleep 10 & but the job does not appear when you run jobs. What is the likely problem?
medium
A. You ran the command in a different shell session
B. The sleep command does not support background execution
C. You forgot to add & at the end
D. The jobs command is not installed

Solution

  1. Step 1: Understand job control scope

    Background jobs are tracked per shell session. Running jobs in a different session won't show jobs from another.
  2. Step 2: Check other options

    Sleep supports background execution, & was used, and jobs is a built-in command, so those are unlikely issues.
  3. Final Answer:

    You ran the command in a different shell session -> Option A
  4. Quick Check:

    jobs show current shell's background tasks only [OK]
Hint: jobs only shows current shell's background tasks [OK]
Common Mistakes:
  • Assuming jobs shows all system background tasks
  • Thinking sleep can't run in background
  • Believing jobs is an external command
5. You want to run a script backup.sh in the background and bring it to the foreground later. Which sequence of commands is correct?
hard
A. ./backup.sh & fg %1
B. ./backup.sh && fg
C. ./backup.sh & jobs; fg %2
D. ./backup.sh & bg %1

Solution

  1. Step 1: Run the script in background

    Use ./backup.sh & to start the script in the background.
  2. Step 2: Bring the background job to foreground

    Use fg %1 to bring job number 1 (the backup script) to the foreground.
  3. Step 3: Analyze options

    ./backup.sh & fg %1 correctly runs the script in background and then brings job 1 to foreground. ./backup.sh && fg uses logical && instead of &, so runs in foreground. ./backup.sh & jobs; fg %2 uses wrong job number %2. ./backup.sh & bg %1 uses bg which resumes background jobs, not foreground.
  4. Final Answer:

    ./backup.sh & fg %1 -> Option A
  5. Quick Check:

    Run with & then fg %job_number to foreground [OK]
Hint: Use fg %job_number to bring background job forward [OK]
Common Mistakes:
  • Using && instead of & for background
  • Using bg instead of fg to foreground
  • Using wrong job number like %2
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard