What is bg command in Linux. How to execute jobs in background in Linux.

In most of the Linux distributions we have bg (background) command. It is used to resume the jobs suspended or stopped in background in the server. In bg command we have very few options or examples to remember.

First of all before displaying the background jobs in the foreground let me explain how those jobs are executing as background jobs or processes. Whenever if you want to execute a long running job, it is idle to schedule it in background as it will take more time. In this case instead of executing it in foreground we can execute that job as background job by appending & symbol as the parameter in the command line for your job.

While executing any job or process if you press Ctrl + Z in keyboard then the process is in suspended state. To restart this job and execute it in background, use bg command.

Below is the example of putting a stopped/suspended job in background using bg command. I am executing sleep command in the terminal.

Stop a process or job using Ctrl + Z:

While executing below print_numbers.sh shell script we stopped it using Ctrl + Z. Now the script execution is in stopped state as it mentioned at the end of the output as [1]+ Stopped sh print_numbers.sh. Here [1] means job id and + means that is the latest or current job.

$ sh print_numbers.sh

Test line 0
Test line 1
^Z
[1]+  Stopped                 sh print_numbers.sh

Now check the jobs command output.

$ jobs

[1]+  Stopped                 sh print_numbers.sh

Add stopped/suspended job in background using bg command:

Above mentioned job is in stopped state. Now we are adding/resuming that job in background using below command and it will start executing the script.

$ bg %1

[1]+ sh print_numbers.sh &

Now check the jobs command output. If you observe the status of the shell script is updated to running from stopped.

$ jobs

[1]+  Running                 sh print_numbers.sh &

See also:

BG Command options:

As we mentioned earlier bg command have less number of options to work with. Below are the bg command notations to represent the background jobs.

How to list the background or stopped/suspended jobs:

To get the list of background jobs use jobs command as below. In below output latest job contains + (plus) symbol in its job id or number and previous job contains – (minus) symbol in its job id or number.

$ Jobs

[1]-  Stopped                 sh print_numbers.sh
[2]+  Stopped                 sleep 1000s

If any of the jobs status is stopped/suspended, then use below bg command notations to restart those jobs.

Background command notations:

%nIt is represent the background process id or number. Using this number you can display the background job information in fg command. We will discuss about fg command later in detail. (n = 1,2,3,4,5,…….n)
%%It is representing current job. By using %% we can put the current job in background.
%+It is same as above option %%.
%-It is representing previous job in the jobs output. By using %- we can put previous jobs in background.

BG command examples:

How to execute a job in background:

Use bg %1 (1 means job id. If you want to add another job id use that id here) to execute a job in background.

$ bg %1

[1]+ sh print_numbers.sh &

How to execute latest job in background:

Use bg %% to execute the latest job from the jobs list output in background. You can use %+ also to execute latest job in background.

$ bg %%
$ bg %+

How to execute previous job in background:

Use bg %- to execute the previous job from the jobs list output in background. For previous job ids – (minus) symbol is mentioned. For current job ids + (plus) symbol is mentioned.

$ bg %-

For more information about bg command in Linux please check the manual page for bg command.

See also:

Leave a Comment