FactorPad
Build a Better Process

Linux bg Command Summary with Examples

Linux bg allows you to move a process to the background.
  1. Purpose - Learn what bg is for and how to find help.
  2. Options - Review a few common options and arguments.
  3. Examples - Walk through code examples with bg.
  4. A tip - Finish off with one more insight.
face pic by Paul Alan Davis, CFA
Updated: February 24, 2021
In this tutorial on Linux bg, 60 of 100, below find a 3-4 minute introductory video, a text-based tutorial and all of the code examples from the video.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / bg-command.html


An ad-free and cookie-free website.


The Linux bg command

Intermediate

Learn to move processes to the background at the Linux command line.

Video Tutorial

Videos can also be accessed from the Linux Essentials Playlist on YouTube.

Linux bg Command Summary with Examples (3:48)

Video Script

The Command and Why You Need It

Our sixtieth word, or command to memorize is bg from our category System.

bg allows you to move a process to the background.

Common Linux bg Options
-options description
%JOBSPEC Run process numbered JOBSPEC in the background

Recall from the last video on kill, we terminated a process that was working in the background, so here we'll see how to push a process back.

Before we start, it helps to think of commands as mini programs and most follow this structure: command -option(s) argument(s).

The bg command has no options and the argument is what is called a JOBSPEC, or an identifier of jobs.

Unlike most commands, help is not available with double-dash --help as bg is a shell built-in (builtin) on most Linux distributions. While exhaustive, the man bash page has helpful details on the topic of job control.

The JOBSPEC number refers to the process number, and we'll use this % symbol to prioritize the jobs.

So why is bg an important command? Well, computers are multitasking, so you'll need to send a process to the background so you can start another. And now you know how to do that.

Demonstration

Okay, the best way to embed this in your memory is by typing in your own terminal window.

Find this on your Mac using a program called Terminal. On Linux use Terminal or Konsole, and currently Microsoft is adding this functionality to Windows.

Here we go. From the last video, we saw ps shows our two processes running.

$ ps PID TTY TIME CMD 211 pts/1 00:00:00 bash 292 pts/1 00:00:00 ps

So let's kick off a process two ways. The first way will be a little slower, and here we'll use sleep because you can identify the run time for the sleep command, and let's sleep for 10 minutes sleep 10m. That would be nice.

$ sleep 10m _

Next, since it's running in the foreground now, let's pause it with Ctrl-z.

$ sleep 10m ^Z [1]+ Stopped sleep 10m

Then use bg %1 for JOBSPEC [1].

$ bg %1 [1]+ sleep 10m &

So it's running in the background.

$ ps PID TTY TIME CMD 211 pts/1 00:00:00 bash 297 pts/1 00:00:00 sleep 306 pts/1 00:00:00 ps $ clear

And last, here's a shortcut for starting and pushing another job right to the background, and sleep 10s with an ampersand &.

$ sleep 10s & [2] 316

And see if you can follow the logic on JOBSPEC [2] as I run a few ps commands.

$ ps PID TTY TIME CMD 211 pts/1 00:00:00 bash 297 pts/1 00:00:00 sleep 316 pts/1 00:00:00 sleep 321 pts/1 00:00:00 ps
$ ps PID TTY TIME CMD 211 pts/1 00:00:00 bash 297 pts/1 00:00:00 sleep 316 pts/1 00:00:00 sleep 326 pts/1 00:00:00 ps
$ ps PID TTY TIME CMD 211 pts/1 00:00:00 bash 297 pts/1 00:00:00 sleep 316 pts/1 00:00:00 sleep 331 pts/1 00:00:00 ps
$ ps PID TTY TIME CMD 211 pts/1 00:00:00 bash 297 pts/1 00:00:00 sleep 336 pts/1 00:00:00 ps [2]+ Done sleep 10s
$ ps PID TTY TIME CMD 211 pts/1 00:00:00 bash 297 pts/1 00:00:00 sleep 341 pts/1 00:00:00 ps

Very good.

A Final Tip

Okay now you know how to use bg. And you know the syntax for commands, options and arguments.

One last tip about the bg command. So while you're reading the bash manual, see how the fg command brings processes forward.

Okay, thanks for visiting today. I hope this was a helpful introduction to the bg command.


Learn More About The Series

For an overview of the 100 videos, the 8 quizzes, a cheat sheet, the categories and a Q&A section, visit:


What's Next?

Sometimes this text paired with video is just better, especially as the tutorials get more difficult. Connect at YouTube and see why.

Outline Back Tip Next

/ factorpad.com / tech / linux-essentials / bg-command.html


linux bg command
linux bg
bg linux
linux background
linux run in background
linux jobs
linux background process
jobs linux
linux run command in background
linux fg
bg unix
linux bg tutorial
linux ps
linux jobspec
shell background
linux bg tutorial
linux bg examples

A newly-updated free resource. Connect and refer a friend today.