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

Improve Windows Security By Closing Open Ports

Improve Windows Security By Closing Open Ports : " A standard Windows operating system has a number of ports open after installation. Some of these ports are needed for the system to function properly while others might not. These ports can pose a security risk as every open port on a system might be an entry point for a malicious user. A port basically allows communication to or from the device. Characteristics are a port number, an IP address and a protocol type. This article will give you the tools at hand to identify and evaluate the open ports on your Windows system to make a decision in the end whether they can or should be closed or left open. Software programs and tools that we will use: CurrPorts : Available for 32-bit and 64-bit editions of Windows. It is a port monitor that displays all open ports on a computer system. We will use it to identify the ports and the programs that are using them. Windows Task Manager: Also used to identify the programs and link some p

Diagnosing a Blue Screen of Death Error in Windows

Diagnosing a Blue Screen of Death Error in Windows : For many years now the famous Blue Screen of Death (BSoD) has been the ultimate indication that something disastrous has happened to make your computer die, but how useful is the information in the BSoD and the respective crash dump file that Windows produces? The best article I ever found explaining the BSoD in depth is here on the Microsoft website, however it’s quite technical and doesn’t discuss how to actually troubleshoot a problem. The crash dump file is just technical details of what was being held in the computer’s memory at the time of the crash, and this will include details on every driver and service that was loaded, and every piece of software that was running. The most useful pieces of information are to be found on the BSoD itself and are highlighted on the screenshot below. These are the BSoD error name, the stop error code and the name of the driver or service that has failed (this last one might not always appea

Use BGInfo to Build a Database of System Information of Your Network Computers

Use BGInfo to Build a Database of System Information of Your Network Computers : " One of the more popular tools of the Sysinternals suite among system administrators is BGInfo which tacks real-time system information to your desktop wallpaper when you first login. For obvious reasons, having information such as system memory, available hard drive space and system up time (among others) right in front of you is very convenient when you are managing several systems. A little known feature about this handy utility is the ability to have system information automatically saved to a SQL database or some other data file. With a few minutes of setup work you can easily configure BGInfo to record system information of all your network computers in a centralized storage location. You can then use this data to monitor or report on these systems however you see fit. BGInfo Setup If you are familiar with BGInfo, you can skip this section. However, if you have never used this tool, it takes ju