Sessions and detached tasks
Quick answer
A background task, a hangup-resistant command and a supervised service solve different problems. Choose the simplest mechanism that meets the task lifecycle.
Prepare the practice environment
Use an ordinary user in an authorized disposable Linux VM. From the extracted package root run python3 lab-fixtures/processes/setup.py, enter its printed path and check pwd. Run each executable example in a fresh Bash process with LC_ALL=C and set -o pipefail. Do not use sudo. For interactive examples use a fresh interactive Bash and enter one command at a time. Signal only the just-started bounded child, never an arbitrary PID. Wait for completion before discarding the private workspace. Skip absent tools.
How it works
A session contains process groups and may have a controlling terminal. A terminal closing can lead to SIGHUP; actual behavior depends on the shell, terminal and session manager. nohup starts a command with SIGHUP ignored and may redirect terminal-connected streams. It does not automatically background the command; use & explicitly if needed. disown -h in Bash marks a known job so the shell does not send it HUP; it does not change all external session policies. Neither provides restart, readiness, logging rotation or survival across reboot. A service manager can supervise a process according to a configured policy. Use the later systemd chapter for persistent services. The tests below check output and exit, not actual terminal disconnect.
The foreground nohup example runs a finite command with its streams explicitly redirected; the next example adds & and waits for the child. These checks establish file output and completion, not survival after logout. In the session inspection, SID and PGID are two different identifiers: processes can share a session yet belong to different process groups. A production service additionally needs an explicit lifecycle policy, controlled logs and health checks, which this short task does not demonstrate.
Try the examples
nohup bash -c 'printf "report ready\n"' </dev/null > report.txt 2> report.errcat report.txtExplicit stream redirection avoids relying on nohup.out. The command remains foreground.
Expected stdout:
report readynohup sleep 0.1 </dev/null > task.out 2> task.err &child=$!wait "$child" && printf 'task completed\n'The ampersand, not nohup, creates the asynchronous job.
Expected stdout:
task completedps -p "$$" -o sid=,pgid= | awk 'NF == 2 {print "session and group available"}'The numeric session and process group IDs vary.
Expected stdout:
session and group availableCommon mistakes
Do not treat nohup or disown as reliable service supervision. Never close your only administrative session to test an unreviewed task.
Practice
Run a finite nohup command with explicit stdout and stderr files, wait for it, then explain which additional capabilities a service manager would provide.
Related references
Study: nohup, disown. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Sessions and detached tasks — Knowledge Check
Apply process lifecycle and job-control concepts to a bounded child-process lab.
Does nohup automatically background a command?
What does nohup normally ignore?
Which mechanism provides configured restart policies?
What does a session contain?