When working in a Linux terminal, running a command normally blocks your session until the task completes. If you are extracting a massive archive or running a complex script, you are forced to sit and wait. However, bash provides a built-in job control system that allows you to pause running commands, push them into the background, and pull them back into the foreground at will.
The three fundamental commands for managing this workflow are jobs, fg (foreground), and bg (background).
Starting a Job in the Background
The easiest way to start a process in the background is to append an ampersand (&) to the end of the command.
tar -czf backup.tar.gz /var/www/html &
The terminal will immediately return a job number in brackets and the Process ID (PID), for example: [1] 48293. Your prompt will return instantly, allowing you to run other commands while the backup continues silently in the background.
Suspending a Foreground Job
If you start a long-running command normally (without the &) and suddenly realise it is going to take a long time, you do not need to cancel it and start over. You can suspend (pause) it.
Press Ctrl+Z.
The terminal will print a message like: [1]+ Stopped tar -czf backup.tar.gz /var/www/html
The process is now frozen in memory. It is not using CPU cycles, but it has not been cancelled.
Resuming a Suspended Job with bg and fg
Once a job is suspended, you have two choices: resume it in the background, or bring it back to the foreground.
Using bg (Background)
To resume the suspended job but let it run in the background (giving you back control of the terminal), use the bg command:
bg
The job will seamlessly resume execution, acting exactly as if you had started it with an & originally.
Using fg (Foreground)
If you want to bring a background job (or a suspended job) back to the foreground so you can watch its output or interact with it, use the fg command:
fg
The job will take over your terminal session once again.
Managing Multiple Jobs with the jobs Command
If you have pushed multiple tasks into the background, you need a way to keep track of them. The jobs command lists all background and suspended processes associated with your current terminal session.
Type jobs and press Enter. You will see output similar to this:
[1]- Running sleep 300 &
[2]+ Stopped nano /etc/nginx/nginx.conf
[3] Running wget https://example.com/largefile.iso &
- The number in brackets (e.g.,
[2]) is the Job ID. - The
+indicates the most recently accessed job (the default forfgandbg). - The
-indicates the second most recently accessed job. - The status will show as either
RunningorStopped.
Targeting Specific Jobs
If you type fg or bg without arguments, they operate on the job marked with the + symbol. To target a specific job from your list, append a percent sign (%) followed by the Job ID.
To bring the wget download (Job 3) to the foreground:
fg %3
To resume the suspended nano editor (Job 2) in the foreground:
fg %2
You can also use these Job IDs with the kill command to terminate a background process without needing to look up its PID:
kill %1
By mastering jobs, fg, and bg, you can drastically improve your command-line multitasking, allowing you to juggle text editors, downloads, and scripts within a single terminal window.