Unlike a graphical desktop environment where you can easily open a dozen different windows side-by-side, a standard Linux terminal is a single, linear interface. If you type a command that takes 30 minutes to execute (like compressing a massive database archive), that single task will completely hijack your terminal window. You will not be able to type any new commands until it finishes. However, Linux possesses a powerful, built-in job control system. By using the bg (background) and fg (foreground) commands, you can pause running tasks, shove them into the background to process invisibly, and reclaim your terminal prompt.
Suspending a Running Foreground Process
Suppose you just executed a massive script (e.g., ./generate_financial_report.sh) and realized it is going to take much longer than expected. You need your terminal back right now.
- While the script is running and blocking your terminal, press Ctrl + Z on your keyboard.
- The terminal will output a message like:
[1]+ Stopped ./generate_financial_report.sh - Your normal bash prompt will instantly reappear.
Crucial Note: Pressing Ctrl + Z does not kill or cancel the program. It merely pauses it in suspended animation. It is currently frozen in the system’s memory, making zero progress.
Sending a Suspended Job to the Background (bg)
To tell the system to un-freeze the program and let it continue working silently behind the scenes, you must use the background command.
- Type the command:
bg - Press Enter.
The terminal will confirm that job [1] has been resumed in the background. Your bash prompt remains available, meaning you can continue typing new commands, installing software, or reading files while your financial report quietly compiles itself.
Starting a Command in the Background Initially
If you know before you press Enter that a command will take a long time, you do not need to use the Ctrl + Z suspension trick. You can launch a process directly into the background by simply appending an ampersand (&) to the very end of the command.
tar -czf backup.tar.gz /var/www/html &
The system will instantly return your prompt and display the background job ID (e.g., [2] 10543).
Bringing a Job Back to the Foreground (fg)
If you have a job running in the background and you want to pull it back onto your main screen (perhaps you want to see its final output or cancel it properly using Ctrl + C), you use the foreground command.
- First, run the command
jobsto see a list of everything running in the background. It will output something like:[1]- Running ./script_A.sh &[2]+ Running tar -czf backup.tar.gz /var/www/html & - Look at the number in the square brackets. This is the Job ID.
- To pull the tar command back to the screen, type
fgfollowed by a percent sign and the Job ID:fg %2 - Press Enter.
The background process will instantly snap back onto your screen, hijacking your terminal exactly as it did when it first started.