βš™οΈ Linux/Unix: Process & Job Control
Estimated reading: 3 minutes 121 views

βš™οΈ 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, and disown
  • Real-world usage with examples and output

🧠 Foreground vs Background Processes

TypeDescription
ForegroundProcess runs interactively and holds the terminal (e.g., running nano)
BackgroundProcess 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.
  • jobs lists current background/suspended jobs.
  • bg resumes a job in background; fg resumes it in foreground.
  • disown removes 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 :
Share

πŸ”΅ Linux/Unix: Background & Foreground (jobs, bg, fg, disown)

Or Copy Link

CONTENTS
Scroll to Top