bg Command in Linux



The bg command is a robust tool in the Linux space that is essential for controlling background operations. As a Linux user, you can increase productivity and effectively manage your workflow by learning and using the bg command.

  • The bg command is used to resume a suspended job (usually a process placed in the background) and continue its execution in the background. This is especially helpful when you have an ongoing task and want to avoid using up your terminal session.
  • To use the bg command, you must first suspend a job by pressing "Ctrl + Z". This will pause the current process and return you to the command prompt.
  • You can then run the bg command to resume the process in the background, allowing you to continue using the terminal for another task.

Table of Contents

Here is a comprehensive guide to the options available with the bg command −

Benefits of Using bg Command

The following are the benefits that come with utilizing the bg command −

  • Long-running tasks − The bg command is especially beneficial for executing long-running tasks, such as file transfers, backups, or data processing, which can take a considerable amount of time to complete.
  • Scripting automation − The bg command is used in shell scripts to automate the execution of long-running tasks, making your scripts more robust and reliable.
  • Resource management − Running processes in the background can help optimize the use of system resources, such as CPU and memory, by allowing the system to allocate resources more efficiently.
  • Multitasking − By running processes in the background, you can continue using the terminal for other tasks, boosting your productivity and efficiency.

Commands Frequently Used with bg Command

The bg command is frequently used alongside other Linux commands, including −

  • jobs − Shows all currently running processes in the current shell session.
  • fg − Moves a background process to the foreground for direct interaction.
  • kill − Terminates a background process.
  • sleep − Used to pause the execution of a script or process for a specified amount of time.

By understanding the bg command and its integration with these related commands, you can efficiently manage and control process execution in a Linux space.

Syntax of bg Command

The basic for the bg command is as follows −

bg [job_spec]

job_spec − It specifies the job to be resumed and it can be a process ID (PID) number, or you can use one of the following symbol combinations −

  • %n − Job number n
  • %str − Job started by a command beginning with str
  • %?str − Job started by a command containing str
  • %% or %+ − Current job
  • %- − Previous job

bg Command Options

Tag Description
[PID] If PID is specified, the jobs with the specified group ids are put in the background.

Examples of bg Command in Linux

In this section, we'll explore various examples, which can help you understand how to use the bg command effectively.

  • Starting a Process in the Background
  • Sending a Running Process to the Background
  • How to Stop and Resume Jobs in the Background?
  • Bringing a Background Job to the Foreground

Starting a Process in the Background

To start a process in the background, run the following command −

Starting Process in Background bg Command

This command tells the system to pause or “sleep” for 120 seconds. By adding the &, the sleep command runs in the background.

This means it doesn’t take over your terminal, allowing you to continue using the terminal for other commands while sleep is still running.

Sending a Running Process to the Background

If you start a sleep command without the "&" (e.g., sleep 120), it runs in the foreground, occupying the terminal.

Sending Running Process to Background bg Command

How to Stop and Resume Jobs in the Background?

You can suspend a job by pressing "Ctrl + Z", which stops the process and puts it in the background as a stopped job.

You can also use the following command for the same case −

kill -s stop jobID

For instance, run the ping command in the foreground −

ping www facebook.com

To suspend the ping command job hit the Ctrl-Z key sequence.

Stop and Resume Jobs in Background 1

Now, use job number 2 to resume the ping facebook.com job by running −

bg %2
Stop and Resume Jobs in Background 2

We'll run a text editor in the background. Start gedit in the background −

gedit &'
Stop and Resume Jobs in Background 3

To stop the gedit job, run with job ID #1 (or PID #93909) −

kill -s stop %1

Or,

kill -s stop 93909
Stop and Resume Jobs in Background 4

To resume the stopped gedit job in the background, run the following command −

bg %1

Or,

bg %gedit
Stop and Resume Jobs in Background 5

To list all background jobs in the current shell session, run the following command −

jobs

Or,

jobs -l
Stop and Resume Jobs in Background 6

Bringing a Background Job to the Foreground

To bring the first background job to the foreground, run the following command −

Bringing Background Job to Foreground

Conclusion

The Linux bg command is an essential tool for managing and controlling background processes, facilitating efficient multitasking, and enhancing workflow.

By mastering this command, you can take full advantage of its features such as running long-duration tasks, handling multiple jobs at once, and improving system performance.

Effectively utilizing the bg command requires a solid understanding of executing and managing background processes in Linux. Learning to initiate processes in the background allows you to keep the terminal free for other tasks.

Advertisements