βοΈ Linux/Unix: Background & Foreground Process Management β jobs, bg, fg, disown Explained
π§² Introduction β Why Learn Background & Foreground Process Control?
In Linux/Unix, you donβt always need to wait for one command to finish before running the next. You can run tasks in the background, switch them back to the foreground, or detach them entirely. Understanding commands like jobs, bg, fg, and disown helps in multitasking efficiently in terminal environments.
π― In this guide, youβll learn:
- The difference between background and foreground processes
- How to move tasks between states using
jobs,bg,fg, anddisown - Real-world usage with examples and output
π§ Foreground vs Background Processes
| Type | Description |
|---|---|
| Foreground | Process runs interactively and holds the terminal (e.g., running nano) |
| Background | Process runs behind the scenes, terminal remains free |
π 1. jobs β View Background Jobs in Current Shell
β Syntax:
jobs
π Description:
Lists all jobs started in the current shell that are stopped or running in the background.
π§ͺ Example:
sleep 100 &
jobs
π€ Output:
[1]+ Running sleep 100 &
π§ Youβll see job numbers like [1], which are used by bg, fg, and disown.
π 2. bg β Resume a Suspended Job in Background
β Syntax:
bg %[job_number]
π Description:
Resumes a suspended job (paused with Ctrl+Z) in the background.
π§ͺ Example:
ping google.com
# Press Ctrl+Z to suspend
bg
π€ Output:
[1]+ ping google.com &
π§ 3. fg β Bring Job Back to Foreground
β Syntax:
fg %[job_number]
π Description:
Brings a background job back to the foreground for interactive control.
π§ͺ Example:
fg %1
π€ Output: The job resumes in foreground with full terminal control.
π« 4. disown β Detach a Job from Current Shell
β Syntax:
disown %[job_number]
π Description:
Removes a job from the shellβs job table, so it wonβt be terminated when the shell closes.
π§ͺ Example:
sleep 300 &
disown
β
Now, even if you close the terminal, sleep 300 will continue running.
π§ͺ Full Workflow Example
Step-by-step:
sleep 200 # Starts in foreground
Ctrl+Z # Suspend the job
bg # Resume it in background
jobs # Check job list
fg # Bring it back to foreground
Ctrl+Z && disown # Suspend and detach it from terminal
π Summary β Recap & Next Steps
Linux gives you fine-grained control over how and where your processes run. These tools let you multitask, suspend, or detach jobs from terminal controlβespecially helpful during long-running tasks or scripting.
π Key Takeaways:
- Use
&to run a command in the background. jobslists current background/suspended jobs.bgresumes a job in background;fgresumes it in foreground.disownremoves the job from shell controlβgreat before closing a terminal.
β FAQs
β How do I run a command in background directly?
β
Use:
command &
β How do I stop a running command temporarily?
β
Press Ctrl+Z to suspend it.
β What happens if I close the terminal with a running job?
β
The job will be terminated unless it’s disowned or nohup is used.
β Can I bring a background process to the foreground after logout?
β
Only if you use nohup or disown beforehand. Otherwise, the shell will kill it on logout.
β How do I resume a specific job in background?
β
Use:
bg %2
Share Now :
