Last month was mention about the Linux Processes on PIDs , parent and child processes and using the ps command. In this part 2, let see how the background and foreground background process work.

Background and foreground processes
An interactive process can run in the foreground or background. To place a process in the background, you use the command bg or the ampersand symbol (&). This lets you do other jobs while waiting for the command to finish. Let’s say you use the syntax

$ find / filename 2 > /dev/null | grep filename | tee result

A command using this syntax could take a long time to execute, especially if you have a lot of files and are listing several network drives. Only one job at a time can run in the foreground, so no further commands can be issued until the prompt returns. To continue working on the command line while it processes, you can execute the same command in the background, using the ampersand. For example, you could enter the command using the following syntax:

$ find / filename 2 > /dev/null | grep filename | >result &

After the command has been issued, the shell displays a message, which looks similar to the following:

$ find / filename 2> /dev/null | grep filename | >result &
[1] 60913
$

The number after the brackets, 60913, indicates the PID for the process, so the status of the background command can be monitored. You can run numerous commands in the background simultaneously.

When the background command has been completed, you will see a line like this:

[1]+ Done find / filename 2> /dev/null | grep filename | >result

Note that the output of a long job has to be redirected when it’s placed in the background. This prevents difficulties when you use the command prompt, because otherwise the output from the background command would continue to appear as you typed.
You use the bg command to send a command to the background while it is running. Initially, you press Ctrl+Z to stop the process, and then enter the bg command to place it in the background, where it resumes execution. Placing a process in the background clears the system, leaving the command line available for other uses.

If it is an interactive process, a message like this will occasionally appear:

[1]+ Stopped (tty output) telnet localhost

If a command is stopped, you can start it running again in the background using the bg command, as in

$ bg %1

To maintain control of the command and to return it to the foreground, you enter a percent symbol (%) before the command’s job number. You then use the fg command to return the process to the foreground, as in the following example:

$ fg %1

As a summary of linux processes part 1 and part 2. Processes perform tasks specified at the command line in Linux. Each process can initiate other processes. Process identity numbers (PIDs) let the shell keep track of which particular process it’s working on. The ps command displays information about each of the processes running under the shell. The bg command enables you to place a process in the background, so that you can do other jobs while waiting for the command to finish. A PID preceded by a percent symbol (%) and used with the fg command returns a process to the foreground.

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock