How to use the bg Command on Linux

In this guide, we will be showing you how to use the bg command on a Linux-based system.

bg command on Linux

The bg command allows you to run a job in the background by specifying its job ID. The most commonly used shells have support for this functionality.

Alongside both the fg command and the jobs command, this tool is a core part of the job control system. This system is what allows you to multitask within a shell session.

Using this built-in utility, you can resume a suspended task within the current shell session. Suspending a process can be done easily by using CTRL + Z while running in the foreground.

Over the next few sections, we will explore how you can use the bg command on your system.

Syntax of the bg Command

The syntax for the bg command only requires you to know the ID of the job that you want to resume. However, there are other ways that you can use this command without knowing the ID.

Below you can see the straightforward syntax of this function that is built-in to the shell.

bg [JOBID]

Using the bg command without using the optional parameter will resume the current job. The current job is the last job sent to the background or suspended.

The “[JOBID]” parameter allows you to select the job you want to resume. There are various symbols that you can use here to select a job.

  • %JOBID – If you know the ID of the job you want to resume, then all you need to use is the percentage sign, followed by the ID.

    You can retrieve a list of the jobs and their ID by using the jobs command.

    For example, if we wanted to resume a suspended job with the ID of 4, we would use “%4” as the job id.
  • %STRING – You can also select a job for the bg command by using a string. This string must match at least a partial part of the command.

    For example, to match a ping command, you would use “%ping” in place of the job ID.
  • %+ or %% – The bg command supports shorthand to select the current job. The current job is the one that you last placed into the background.

    You need to use the percentage symbol (%), followed by the plus symbol (+). Alternatively, you can also use a double percentage sign (%%) to achieve the same task.
  • %- – Selecting the previous job to resume is straightforward as well. All you need to do is use the percentage sign (%) followed by the minus symbol (-).

    The previous job is the one used before the job that is now marked as the “current” job.

Examples of Using the bg Command

Let us run through some simple examples to give you an idea of how the bg command works.

For this example, we suspended a few applications by using CTRL + Z in the terminal while they were running. You can also use the ampersand (&) at the end of a command to keep it running and place it into the background.

pi@pimylifeup:~ $ jobs
[1]-  Stopped                 ping google.com
[2]   Running                 chromium-browser &
[3]   Stopped                 nano exampletext.txt
[4]+  Stopped                 ./examplescript.sh
[5]   Stopped                 nano test

Resuming a Job By Using its ID

The primary way that you will use the bg command is by referencing the job you want to resume by its ID.

Thanks to the jobs command, you can easily get the ID for the job you want to resume. Additionally, when you suspend a job, its ID will be automatically printed to the command line.

For this example, let us say that we wanted to resume our ping command by using the ID “1“. This means all we need to do is use “bg” followed by %1“.

bg %1

Using a String to Select a Job with the bg Command

The bg command allows you to select a job by using a string. This string must be either part of or the entire command.

For this example, if we wanted to resume our bash script, we would need to use the percentage sign (%), followed by at least the start of the name.

As shown below, we can get away with only including part of the command that started that job. In this case, we will use “./example“.

bg %./example

Selecting the Current Job using the bg Command

The utility allows us to use shorthand symbols to select the current job. These are useful if you only want to deal with the recently suspended command.

The default behavior of the bg command is to utilize the current job. So you can simply just type in “bg” into the terminal.

bg

You can also use the percentage sign (%) followed by the plus symbol (+) to select the current job.

bg %+

Alternatively, you can also use two percentage signs (%%). This will also start the job in the background.

bg %%

Resume the Previous Job

The bg command also has additional functionality to select the “previous” job. This previous job is the one that you ran before the “current” job.

You can start the previous job in the background using the bg command, followed by a percentage sign (%) and a minus symbol (-).

bg %-

Sending a Process to the Background

Whenever you typically run a command within a shell like bash it will automatically run in the foreground. However, there are a couple of ways to run this in the background instead.

Using the Ampersand &

When you run your command, including an ampersand (&) at the end tells the shell it should run it in the background.

For example, if we use the following command, the ping command will continue to run in the background, but we will be able to continue to interact with the terminal.

ping google.com &

Suspending the Current Process

You can also suspend the process you are currently running in the terminal. This process must be done while the process is still in the foreground.

All you need to do to suspend a process within the shell is press CTRL + Z on your keyboard.

Conclusion

Hopefully, you now have an idea of how to use the bg command on your Linux system.

This command is a crucial part of the job control system. Combined with other commands such as fg and jobs, you can multitask within your current shell session.

If you have any questions about using the bg command, please comment below.

You can also check out our other Linux command guides, as well as our Linux tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *