FactorPad
Faster Learning Tutorials

Linux bg command summary with examples

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.
Paul Alan Davis, CFA, November 1, 2016
Updated: August 4, 2018
In this tutorial, 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 Next

~/ home  / tech  / linux essentials  / bg command


The Linux bg command

Intermediate

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

Video Tutorial

Linux bg command summary with examples (3:48)

Videos can also be accessed from the YouTube Playlist.

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.

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 video is just better. Subscribe to our channel here and give it a try. Especially as the tutorials get more difficult.

  • For the Outline to all 100 commands, click Outline.
  • To go back to the kill command, hit Back.
  • Did you know you can change shell programs? To learn how with chsh, click Next.

Outline Back Next

~/ home  / tech  / linux essentials  / bg command



 
 
Keywords:
linux bg command
linux background job
linux background process
jobs linux
linux bg tutorial
linux bg examples
linux jobs
background process linux
run command in background
unix bg
linux fg
linux ps
foreground process linux
shell background jobs
fg command
jobspec linux