Sending a job to the background and back to the foreground in Linux - gHacks Tech News

Sending a job to the background and back to the foreground in Linux

From the Linux command line you can send a running job into the background which will return your bash prompt to you. You might need that prompt to quickly run another command, or you might just want to let that command run in the background for a while and do not need to view the output. Either way, you can send that command to the background and then, when you need it again, bring that same running command back into the foreground. Let's take a look at how this is done.

The job

First things first, I am going to create a very simple job so you can practice this task on. Let's create a file with some very simple contents. Do the following.

  1. Open up a command prompt.
  2. Enter the command echo "this is my test file" > ~/test_file.
  3. Issue the command less ~/test_file to make sure the previous command worked.

You should now have a very simple test file with a single line of content. That's the file we are going to work with.

The commands

There are two commands we are going to work with:

  • bg: Lists the current zombied programs.
  • fg: Brings a zombied command to the foreground.

Before we continue I should probably explain a couple of things. First, a zombied program is a program (or command) that has been sent to the background. In order to do this you must issue the command in the terminal window and, while the command is running, hit the Ctrl-z combination. When you do this the command output (or just the command if it is not returning any output) will disappear and return you to your bash prompt.

So...let's use our sample we created above. If you look at your terminal window, you should still see the contents of the ~/test_file, which will read "this is my test file". That should be the only thing you see. You do not have access to your bash prompt so you can't run any more commands from that terminal window. Hit the Ctrl-z key combination and the contents of the file returns and the contents of the file are "gone". The command less is now in the background.

Now, to see what commands are currently in the background enter the command bg. You should see something like:

[1]+  Stopped                 less  test_file

To bring that command back you would issue the command fg less. That will return the contents of the ~/test_file back to your terminal window. Now, if you close that command (in the case of the less command hit the q key to quit) and you issue the command bg you will not see that particular command listed among any background commands you have.

NOTE: The bg command will not take into account any system daemons you have running. This command ONLY takes into account the commands your particular user has placed into the background.

Final thoughts

You might not be able to come up with any useful need for this nice Linux feature right away. But some time you are going to be working in the terminal window and you will need to do exactly what is prescribed above.

Advertisement

We need your help

Advertising revenue is falling fast across the Internet, and independently-run sites like Ghacks are hit hardest by it. The advertising model in its current form is coming to an end, and we have to find other ways to continue operating this site.

We are committed to keeping our content free and independent, which means no paywalls, no sponsored posts, no annoying ad formats or subscription fees.

If you like our content, and would like to help, please consider making a contribution:


Previous Post: «
Next Post: »

Comments

  1. Dotan Cohen said on January 20, 2011 at 10:32 am
    Reply

    No, a zombie program that still has an entry in the process table despite having finished running. See here:
    https://secure.wikimedia.org/wikipedia/en/wiki/Zombie_process

    What you are referring to is a background process

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

Please note that your comment may not appear immediately after you post it.