Manage work that takes time
By the end: Run a background job, wait for completion, and distinguish terminal sessions from scheduled services.
The idea
Bash jobs belong to a shell session; they are not the same as system services. & starts a command asynchronously, $! identifies the last background process, and wait collects a child's result. Interactive job control differs from noninteractive scripts. Schedulers run with a different environment and need explicit paths and logging.
Your command toolkit
jobs
What it solves: Inspect jobs in this shell.
jobs -l
Lists the current shell's jobs and PIDs. It does not show every process on the machine.
bg
What it solves: Resume a stopped job in the background.
help bg
In an interactive practice shell, Ctrl+Z stops a foreground job and bg resumes it. Check jobs before choosing a job number.
fg
What it solves: Bring a job back to the terminal.
help fg
fg makes a selected job foreground again. Use jobs to identify the intended job rather than assuming %1 belongs to your exercise.
wait
What it solves: Collect a background child's result.
sleep 1 &
si_job=$!
wait "$si_job"
wait returns when the child finishes and exposes its exit status. It is useful for synchronizing script steps.
nohup
What it solves: Ignore hangup for a short background task.
nohup sleep 2 > /dev/null 2>&1 &
nohup changes hangup handling; & is what backgrounds the job. It does not restart failures or provide full service management.
screen
What it solves: Keep an interactive terminal session available.
screen -ls
Lists your existing sessions. An optional screen session can be detached and reattached; availability and access are still tied to the host.
sleep
What it solves: Pause for a bounded interval.
sleep 1
A delay is not proof that another service is ready. Prefer checking the required condition in real automation.
watch
What it solves: Repeat a read-only check.
watch -n 2 date
Runs date every two seconds until Ctrl+C. Avoid placing a modifying command inside a repeated watch loop.
time
What it solves: Measure elapsed work.
time sleep 1
Compare wall-clock, user CPU, and system CPU time. Sleeping takes elapsed time with little CPU work.
crontab
What it solves: Inspect scheduled user jobs.
crontab -l
Lists the user's crontab if one exists. A five-field schedule uses minute, hour, day of month, month, and day of week; editing installs recurring work.
Before you move on
Try the guided lab below. Compare the result with the explanation, then record your reasoning in the workbook. A missing optional program or a restricted environment is something to explain, not a reason to change system settings blindly.
Practice with evidence
- Predict the result
- Run the guided lab
- Compare the output
- Explain your choice
A worked example
Guided lab
sleep 2 &
si_job=$!
jobs -l
wait "$si_job"
si_status=$?
printf 'Finished with status %s\n' "$si_status"
What to expect: A background job appears, then wait completes with status 0 after roughly two seconds.
Make it yours: Explain the roles of &, $!, and wait. Sketch a daily reporting job with an explicit executable path, output destination, and timezone assumption; do not install it yet.
Read further
- GNU Bash reference manual ↗www.gnu.org
Apply what you learned
Your private practice
Answers are private. Completion requires the correct knowledge-check answer and a written response when an activity is included.
Restore a downloaded workbook
Choose a recovery file for this course and version. Review the recovered copy before saving it to your account.
Ask a question or report a problem
Describe what needs attention. Your private answer is shared only if you select the option below.