In Linux, the bg command helps in managing jobs by enabling users to put existing jobs in the background and continue to run as a background job. It is widely known as the 'background' command which is one of the job control commands used in the Unix-like operating system.
In this tutorial, we learn about bg command in Linux.
Prerequisites
Access to a terminal
A normal user or sudo user account.
Linux bg command
The core function of the bg command is to send the foreground service to the background using job control. This command can be used on a running foreground job to suspend and resume in the background. On the other hand, when the bg command operates on suspended jobs, they will be restarted as background jobs.
It is useful to know a bit of a background on how the Linux jobs work. Usually, the currently running jobs can be suspended by the key combination ctrl + z. It would stop all the jobs that are currently running in the foreground and give the control back to the shell. Also, the suspended job will be assigned with a job id.
The bg command can be originated from two sources such as an external utility program or system shell built-in. it can be verified with the type command as follows.
type bg
In this example, the bg command is available as a shell builtin which can be used to send foreground jobs associated with the current shell session to the background. In some cases, it might available from both the sources: a shell built-in and an external utility program.
syntax
The following is the basic syntax of the bg command.
bg [job_specification]
The job_specification argument is optional. If you call the bg command without any argument, it will push the current job that is running in the foreground to the background or resume the suspended job in the background.
bg command example
The bg command works with all the Linux shells except the bourne shell. Hence, we will be using the bash shell to execute the following commands. Since it operates on jobs associated with the shell, we need some jobs running. The jobs command displays the job id, process id, and status of the associated jobs with the current shell session.
jobs
This command returns nothing because we got no jobs associated with this shell session.
The basic function of the bg command is to send a foreground job to the background. Let's initiate a sleep command to see how the bg command can be used to send a foreground job to the background.
sleep 60
This will create an empty job in the system memory for 60 seconds and the control over the command line will be lost immediately. Let's suspend the job by pressing the ctrl+Z keys on the keyboard as shown in the following.
Next, we will call the bg command to resume the suspended sleep job in the background. As discussed, the stopped job number should be passed to the bg command as follows. The job number is 1 in this example.
bg %1
Now the job identified by the ID 1 will be resumed and goes into the Running status. Let's verify it by executing the jobs command.
Let's verify the behavior of the bg command when the job specification or job id is not passed. It should operate on the current job and resume it in the background. First, we will initiate a sleep command for 120 seconds and suspend it. The same procedure will repeat for another sleep job which times out after 240 seconds.
Finally, we will be calling the bg command without passing the job_spec(job ID) argument. So, the command should operate on the current job which is the sleep command issued with a timeout of 240.
As you could see, the immediate sleep command(with 240 timeout) has been resumed in the background.
bg options
The bg command comes with two main options listed as follows.
The job_spec option
A job associated with the current shell session can be referenced using a job_specification. The job_specification can be specified in different forms as follows. Furthermore, it should be started with a % character.
The %+ and %- characters can be used to reference the current job and the previous job associated with the current shell environment.
In referring to the previous example, We can use the following command to resume the previous sleep job which has a 120 seconds timeout.
bg %-
Furthermore, you can use the job id directly with the bg command. Let's resume the sleep job with 120 seconds timeout that is identified by job id 1. You should specify the stopped job number as follows.
bg %1
It is possible to reference a job by the command that is used to start it. Since the sleep command starts with the letter s, you can specify it as follows.
bg %s
In addition, the command which is used to start the job containing a string ‘s’ can be specified as follows.
bg %?s
The --help option
The '--help' option can be used to display additional information related to the bg command as shown in the following.
Conclusion
In short, the bg command is one of the job control commands that has been used to move a Linux job to the background. As discussed, this command should be available as a built-in shell command. The bg command can be called without any arguments that will move the current job to the background. Also, a specific job can be referenced by specifying the job_specification argument. Overall, the bg and fg command is very useful in managing Linux jobs.
If this resource helped you, let us know your care by a Thanks Tweet.
Did you find this article helpful?
We are glad you liked the article. Share with your friends.
About The Author
Nimesha Jinarajadasa
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called "03 tiers(DB, M-Tier, and Client)". Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.
Comments