Foreground (fg) &Backround( bg) Jobs
1.The Foreground and Background jobs (fg & bg) it allows the user’s to run multiple programs on a terminal.
2.By default the shell allows to execute only one program at a time on the terminal, Now if you want to run another program or command on the terminal we have to wait until the running programs get completed.
Before we get into the practical stuff, let me tell you in short about the foreground and background process.
3.Sending a program in the background is like minimize a running program in Windows operating system and foreground is like restore it back to the screen i.e maximize the program in windows operating system.
Foreground: 1.Is nothing but the program or process that is connected to the terminal
2. A Process is said to be in the foreground since it could connect with the users via the screen, keyboard etc….
Background: Background jobs are not connected to the terminal, So you can place a program in the background that doesn’t require any user interaction.
When do we need to send a program to the background?
For example, The administrator is performing a backup task and that task would take more than an hour to get complete, after executing the backup program, the terminal will not allow the administrator to run another program ,To run the another program he has to wait until it gets finished, So here when a program it takes more time to finish its task (ex: backup task, capturing network packet tasks and many more..) those programs or commands we should send it to in the background .
Now let us see,how to send a program to background and then how to bring it back to the foreground,
Ex:1 Run a program in the Background:
It’s very easy to perform, all you have to do is add the ampersand symbol at the end of the command,thats all , once you hit the enter button that program will run in the background.
Syntax: <command> &
Let me create a file,
#tail -f /vasanth.txt
If I enter this program this would occupy the entire terminal and it won’t allow us to run another program
Now am going to send this program to the background (by adding ampersand at the end of the program)so that we can run multiple programs on the terminal
#tail -f /vasanth.txt &
This will send the program to the background
Note: (Here I use the tail command, this will make the file in buffering mode on the terminal, In your production environment you can use any program that takes more time to complete)
To check the background jobs details:
After you run a program in the background, now it’s time to check the bg jobs details.
we have a command called “jobs” this will give you a detail report about the bg jobs
will give you the details of the bg jobs like,
2. +/- (job priority)
3.status of the bg jobs and the program name
4. program name
Sample Output:  + Running tail -f /vasanth.txt
Note: 1.Current job always flagged with +
2. The previous job always flagged with –
Ex2: How to Bring the background(bg) jobs to the foreground(fg):
It’s quite easy to do it, all we have to do is use the fg command followed by %(percent) symbol and the background job id
Syntax: fg %<bg id>
Always remember before you bring the bg jobs to the foreground, you should check the background job details by using the jobs command to get the bg id, status etc…
Now once I confirmed the bg job is in running status and also make a note of the bg id, run the command
That’s it……The bg process now it will come in fg.
Ex:3 How to send a running program to the background?
Use the cntrl+z key, this will suspend the running program and move it to the background with “stop ”
Note: If you Press cntrl+c the program gets killed
Now check whether the program is sent back to the background by using the jobs command
Sample Output: + stopped tail -f /vasanth.txt
from the output the fg job successfully moved to the bg, You would see the status as stopped now
the above screen shot says we have a job at the bg with stopped status, What
Now, What we will have to do if I need to bring this job to the foreground?
The first thing you have to bring the job (which is in stopped staus at bg in our case) to the running status and then by using the fg command you could move it back to the foreground.
Sampleutput: [1[+ Stopped tail -f /vasanth.txt
SampleOutput: + tail -f /vasanth.txt &
SampleOutput: 1]+ Running tail -f /vasanth.txt &
Now, move the program to the foreground,
Ex:4 Let me show you the difference between + and – Flagg from the jobs command output
Here am going to run two programs in the background
#tail -f /vasanth.txt &
Sample output:  5525
Sample output:  5526
Now run the jobs command to view the status of all the bg jobs
Sample Output:  - Running tail -f /vasanth.txt  + Running /firefox
So from the above output, you can notice that the second program flagged with the “+” which says this one is the latest job, The first program flagged with the ” -” which says this one is the previous job
If you found this article useful, Kindly Subscribe here 👉🏿👉🏿 Click this link to Subscribe