In this lesson, you will learn how to juggle multiple running programs within a single terminal window without needing to open new tabs.
The Concept of Job Control
Normally, when you run a command like find / -name "*.log", your terminal becomes "blocked." You cannot type new commands because the shell is waiting for that process to finish. This is called a foreground process.
Sometimes, you need to run a task that takes a long time, but you don't want to stop working. You can move that process to the background, allowing the shell to return to an interactive state immediately.
Managing the Flow
You can control the state of your processes using a few simple shell techniques:
-
Start in the background: Add an ampersand (
&) to the end of your command.$ sleep 100 & [1] 1234The shell prints a job number (in brackets) and the PID, then gives you the command prompt back.
-
Pause and background: If a process is already running in the foreground and you realize you need to do something else, press
Ctrl+Z. This sends aSIGTSTPsignal, which pauses the process. To send it to the background, typebg. -
Bring it back: If you want to interact with a background process again, use the
fgcommand. If you have multiple background jobs, you can specify which one by using its job number, likefg %1.
The Terminal Lifecycle
When you send a process to the background, the shell remains the "parent" (or manager) of that job. The process continues to run, but it loses access to your keyboard input. If the process tries to print output, it will still display it in your terminal window, which is why background processes can sometimes clutter your screen. By learning to toggle between fg and bg, you effectively manage the "focus" of your terminal session, keeping your workflow moving without losing control over your long-running tasks.
Key Takeaway: Use & to start a task in the background, Ctrl+Z followed by bg to move an active task to the background, and fg to pull a process back into your immediate control.