Foreground and Background Process(fg & bg)

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,

#touch  /vasanth.txt

#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

#jobs

will give you the details of the bg  jobs  like,

1.bg id,

2. +/-  (job priority)

3.status of the bg jobs and the program name

4. program name

 

Sample Output: [1]  +   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…

#jobs

Now once I confirmed the bg job is in running status and also make a note of the bg id, run the command

#fg  %1

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 ”
status.

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

#jobs

Sample Output: [1]+  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.

#jobs

Sampleutput: [1[+  Stopped tail -f /vasanth.txt

#bg  %1

SampleOutput: [1]+   tail  -f  /vasanth.txt &

#jobs

SampleOutput: 1]+     Running tail  -f  /vasanth.txt &

Now, move the program to the foreground,

#fg  %1

 

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: [1]  5525

#firefox  &

Sample output: [2] 5526

Now run the jobs command to view the status of all the bg jobs

#jobs

Sample Output: [1] - Running   tail -f /vasanth.txt

               [2]  +  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

 

About Author:

Hello readers! Let me introduce my self first. My name is Vasanth Nirmal Singh J S having 9+ years of experience in IT on all flavours of Unix operating systems ,Storage's and many more .. I would like to share my technical experience i have come across - can be help to other people. So in this blog, I'll post my thoughts related to ITIS. I'll share experiences that I've had while working in different environments. You can expect content related to Unix,Solaris,Linux,EMC Storeages,HP-UX and many others. I hope this blog can be useful for you! Your comments will be appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *