Introduction to Linux bg Command
BG command in Linux world stands for the background. It is sometimes essential for a script to run part of the processes in the background after a job is suspended. In some scenarios, it becomes quite a time consuming to work on a set of commands mainly when the commands needs a lot of in-memory processing or some file processing may need some more time than usual or any other process otherwise, one would like to run these process somewhere else or at some other time and do some other execution in the meanwhile. This sounds like opening another terminal and keep executing the scripts, but what in turn will happen that in case a lot of such scripts needs to be run we will end up having multiple terminals. The alternate to this is the background running utility using bg command which in this article we will go though in detail.
What is Linux bg Command?
In Linux, we have the concept of foreground and background. These terms are nothing but ways of execution of scripts in Linux. Running scripts in the foreground is nothing but running it in the terminal making the terminal unavailable for any other execution. And background makes the terminal available for any other command execution.
These method of execution falls under the genre of Job control, which essentially brings in the capability for developers to not only stop or suspend process execution but also resume or continuing the execution as required by the user. This utility is taken care by running the script with & sign. Another big utility is to re-initiate suspended jobs by running them as background jobs. This is what bg command takes care of!
Another essential feature of bg command is the ability to be used as an external or internal command. Internal commands are the ones that are in the context of the shell. This essentially means that there is no other process that needs to be spawned for executing the commands in these commands. Internal commands are generally faster to execute as Linux doesn’t have to look for these commands in the PATH variable. External commands need to be searched for in the PATH variable and these commands are not built-in commands within the shell. What Linux does is, it looks for the file in the PATH variable which are generally /bin or /usr/bin and the executable with that command.
Next, we would learn about the syntax. The basic syntax is as follows:
In case there are more than one job id which needs to be run in the background we would use:
bg id_of_job_to_run1 id_of_job_to_run2 id_of_job_to_run3 …..
Now at this junction, we should be aware of the fact on how do we understand the job id which goes as a parameter to the command bg? In the later section, we would also like to understand how do we suspend or stop jobs currently running in the sessions?
To understand how can we obtain job id in the shell we would run the command jobs or jobs -l which will eventually enlist all the jobs along with the corresponding id beside it. These job ids can be referred to in the shell by introducing % as a job specification.
The following combinations of the symbol can be used for referring to the corresponding job id:
%<Number>: The way this combination is used is by referring to the list of jobs that gets exposed when you pass the command jobs -l, and the output is nothing but a list, with each job id having a serial number. This serial number is exactly what replaces the <Number>. For instance, let us say we have the following output from jobs -l.
1. -1111 Running ping 22.214.171.124
2. 2222 Running AdRaNeAmV.py&
3. + 9999 Stopped abc.py
Now, if we write %3, then essentially, we are referring to job id 9999.
%String: This particular methodology is used if one remembers the name of the executable file. In case we know that in the previous example, there is a python file named as abc_xyz.py, we can just replace <String> with abc or abc_x or any of the substring of the file name. It automatically searches for job description which contains this substring and references it to that. In case there are more than one file with similar substring, we need to be extra careful in replacing the <String> with something very specific to what you want to refer to.
%+ OR %%: This particular referring job id refers to the current job.
%-: Unlike the previous way of referencing, this method is for referring to a previous job id.
The next thing is how do we suspend a job in order to run it in the background. One we know what the process Id we would instantiate either of the 3 ways in order to carry out the appropriate action. The first is to press Ctrl+Z if a command is getting executed in the foreground. The next is to use the command kill -s stop <Process_Id>. Last but not the least way is to use pkill command and the syntax is:
pkill -stop <Process_Id>
Now once we have suspended a particular process, we know what process ID it was initially following. We would now use
which will eventually start the process which was running before we suspended is, in the background!
Examples to Implement Linux bg Command
In this section we will have a peek at the working of Linux bg Command through an example:
Running a script in the background using &:
Running another job and then suspend it by pressing Ctrl+Z.
Output after pressing Ctrl+Z
Listing out jobs.
Re-running a suspended job i.e. the one we suspended by Ctrl+Z and then listing the jobs currently running. Here we would be running the job which is listed by list number 4.
Suspending any other job by using kill command:
kill -s stop 584
In this article we have gone through the usage of bg command in real life through and example and also some concepts on how do run background jobs in general as well. In using this capability one can easily restrict the opening of many terminal windows to run jobs and easily start a terminated job once the resource is available.
This is a guide to Linux bg Command. Here we discuss an overview on the Linux bg Command and its different commands along with Examples and Code Implementation. You can also go through our other suggested articles to learn more –