Pular para o conteúdo principal

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

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.


© Jack Wallen for gHacks Technology News, 2011. | Permalink | Add to del.icio.us, digg, facebook, reddit, twitter
Post tags: , ,

"

Comentários

Postagens mais visitadas deste blog

Favigen, Favicon Generator

Favigen, Favicon Generator : " Favicons are small icons that help identify websites. They are used as a visual representation of a website both in the web browser and at many online services. The three most prominent locations of favicons are the browser’s address bar, the tabbar and the bookmarks folder. Many webmasters like to create custom favicons to add that custom identifier to their website. Favicons can be created in many image and icon editors, but also online. Favigen is a straightforward favicon generator that can turn an image into a favicon. All that it takes is to pick an image from the local hard drive first, select the dimensions of the favicon and click the submit button to make the service generate the favicon. Favigen supports several image formats, including jpg and png, and it does not seem to have size restrictions either. Available image dimensions range are 16×16, 32×32 and 64×64. The generated favicon is displayed directly on the page. A click on do...

A simple rsync script to back up your home directory

A simple rsync script to back up your home directory : " Backing up important data is obviously something we should all do. Unfortunately, it is not always easy to make it happen. We get lazy; we do not have the additional hardware for a backup server; it takes a long time and a lot of CDs to back up to optical media; we do not trust online backup services; backup schemes are difficult to set up and use — any of dozens of reasons can stand in our way. Still, we know we should be backing up our important data. Modern open source Unix-like operating systems offer a plethora of options for incredibly simple, effective backup schemes, however. If the problem is figuring out how to set one up, a simple rsync solution may be exactly what you need. The rsync utility is used to synchronize files between two systems. It does so by way of incremental copies, only copying from the source to the destination what has not already been copied there, saving time, network bandwidth, and syst...

Google Wave now open to the public: faster, Robots and Gadgets aplenty!

Google Wave now open to the public: faster, Robots and Gadgets aplenty! : " Filed under: Internet , Google If you somehow missed it, Google Wave is now a bonafide Labs project: rather than being an invite-only alpha, it's now a public beta test! If you don't already have an account, just head on over to Wave and use your regular Google login details. If you've not seen any of the Google Wave introductory videos , you should check them out -- they explain the whole thing a lot more succinctly than I ever could. Wave has also been enabled for Google Apps domains -- businesses could convert their internal communication to Waves today! Leading up to this public release there have a lot of changes. It's by no means finished, but Google Wave is now a lot faster . It's also more intuitive -- more useful -- and given the large number of Robots and Gadgets now reaching prime-time readiness, it feels like the mass adoption of Wave is imminent. Also, if you're an ol...