In this lesson, you will learn how to shift active tasks between your foreground terminal view and the background to maintain full control over your interactive session.
When you run a command in your shell, it typically occupies your "foreground." This means the process is attached to your terminal's standard input—it’s listening to your keyboard, and any text it prints appears directly on your screen. If you start a process that takes a long time, your terminal becomes "blocked," preventing you from typing new commands.
Moving Jobs to the Background
You can "background" a running process by first suspending it. Press Ctrl+Z on your keyboard. This sends a SIGTSTP signal to the process, freezing it and returning you to your shell prompt. To continue that process in the background, type bg. The process will keep running, but it is now detached from your immediate keyboard input, allowing you to use the terminal for other work.
Bringing Jobs to the Foreground
If you need to check on a background process or interact with it again, use the fg command. This brings the most recent background job back into the foreground, reconnecting its standard input to your terminal.
Managing Access
The terminal acts as a gatekeeper. Only one process can be the "foreground" process at any time; that is the only one permitted to read from your keyboard. If you attempt to send input to a background process, the terminal line discipline will often intervene to protect the state of your session.
Try this sequence to see it in action:
- Run
cat(it will wait for input). - Press
Ctrl+Zto suspend it. - Run
bgto push it to the background. - Try to type; notice your shell ignores the input for
catand processes it for your command line instead. - Run
fgto bringcatback and interact with it again.
This flow allows you to multitask without needing multiple terminal windows, keeping your workspace organized while maintaining strict control over which process "owns" the input stream.
Key Takeaway: Use Ctrl+Z to suspend a task, bg to run it in the background, and fg to return it to the foreground to regain control of your terminal's input stream.