The bg command in Linux helps in managing services by allowing us to put existing processes in the background. Let’s understand the basics of the command and dive right into the usage.
What’s the bg Command in Linux?
The bg command in Linux, better known as the ‘background’ command, is a job control command in Linux / UNIX-like systems. The function of this command is to send a service, which is working in the foreground, to the background using job control. Essentially, job control works to terminate some service or process. Later the system restarts it based on your requirements. This is done with the help of your shell and the operating system.
Syntax of the bg Command
The bg command works for all Linux shells except the Bourne shell. When the bg command is used on a running foreground process, the process is suspended and then resumed in the background. When it is used for a stopped service, the service is restarted as a background service.
The best way to understand any command is through understanding it’s syntax. Here is how the syntax for the bg command in Linux looks like.
The bg command in Linux has only one parameter. This parameter is used to denote the job. There are four different ways in which we can denote a job for the bg command. The following table lists them.
|%n||Sends the job, having Job ID ‘n’, to the background.|
|%% or %+||Sends the current job to the background.|
|%-||Sends the previous job to the background.|
|%string||Sends the job whose name starts with string to the background.|
If a job runs in the foreground, it occupies the shell until it is stopped or suspended. We can use the bg command to send the job to the background allow you to focus on other processes that need your attention.
Using the bg command in Linux
We now have an understanding of the bg command in Linux along with the job parameters. Now it’s time to use this knowledge for practical application of the bg command. For this tutorial, we will go over some examples to learn how to use the bg command.
Sending a job to the background
The core function of the bg command is to send a job running in the foreground to the background. To see how we can send a job to the background, let us initiate a sleep command for 30 seconds.
Since we’re not specifying any commands to run after the sleep, this will create an empty process that just stays in memory for 30 seconds. We won’t be able to use the command line because the shell is occupied. So we will use Ctrl+Z to stop the process. Now, we will use the bg command to resume the sleep command in the background. The following screenshot shows you how this works.
Restarting a stopped job in the background
This is done by feeding the required job ID into the bg command in our command line. However, we need to begin by identifying the particular job which we need to run in the background. We see the list of all jobs using the command jobs.
This command will list all the jobs that were running or stopped in the background. Let’s use job 1 and restart it using the bg command in Linux.
bg %<job ID>
Running the jobs command again shows us that the job has now started running in the background. If you look into the screenshot given below, you should notice that job 1 has started running.
Alternative to the bg command
If you wish to start a process in the background right from the moment it is initiated, we use an alternative to the bg command. This can be done by appending the ‘&’ sign. To demonstrate this, let’s use the sleep command again. We’ll start the sleep command with the “&” sign added to it like below.
sleep 30 &
This will initiate the sleep command and send the job straight to the background. We can verify this using the jobs command. As you can see in the screenshot below, the sleep command is running in the background.
Difference Between the bg Command and the “&” sign
As you notice above that the ‘&’ sign can do the job of sending a process to the background too. So what’s the use of the bg command then?
The major difference is that the bg command in Linux can be used on processes that are already running on the foreground. The ‘&’ sign works only when you’re starting the process and cannot be used for processes that are already running.
So for processes that you haven’t started up yet, the bg command and the ‘&’ sign will behave in the same way.
Any Linux user needs to have an idea about managing services using the bg command in Linux. This enables a person to have greater control over the services on their system. Keeping a process in the foreground prevents you from using the system efficiently for another process which needs more of your attention.
In such a case, the bg command in Linux allows the user to send the less important job to the background. This frees your shell for processes that need to be run in the foreground. We hope this tutorial was able to help you understand the bg command in Linux. If you have any feedback, queries or suggestions, feel free to reach out to us in the comments below.