04. Backgrounds and Foregrounds jobs, bg, fg

Usually, when you start up a process, it takes over the terminal. These processes are thought of as running in the foreground. Let's now learn how to start a process in the background and how to move processes to and from the background/foreground.

Starting a Process in the Background

More often than not we'll want to launch a program right away into our foreground. However, if we plan to run a process that may take a long time, we can place it in the background so we can have still access to the command line interface. This way you can access the shell while it completes.

To start a process in the background, place an ampersand (&) after it.

$ gedit &
[1] 2314

The output it places it called the job number, These identifiers are useful, as we'll later see when we use them to send signals.

Using jobs to check user run processes

To see the list of all processes within the current session (including those in the background), use the jobs command. This is especially useful before logging out of a session to ensure that all processes that you've started have closed.

$ jobs
[1]+  Running        gedit &

The number in brackets is the job ID, with the term Running describing its state.

The job command is useful to make sure all the processes have been properly closed before logging out. If not, client programs may freeze up if a lot of programs are still running.

Moving a Process to and from the Background/Foreground

To move a process to the background from the foreground, use the bg command and its job number.

$ bg 1

To move this back to the foreground, simply input the job number and use that as the argument for the fg command.

$ fg 1

Take your Linux skills to the next level!

The Linux Command Line

Take your Linux skills to the next level! Try Linux & UNIX

The Linux Command Line takes you from your very first terminal keystrokes to writing full programs in Bash, the most popular Linux shell. Along the way you'll learn the timeless skills handed down by generations of gray-bearded, mouse-shunning gurus: file navigation, environment configuration, command chaining, pattern matching with regular expressions, and more.

$ Check price
39.9539.95Amazon 4.5 logo(274+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Acupressure Mat & Pillow

Aching back from coding all day? Try Back Problems

Relieve your stress, back, neck and sciatic pain through 1,782 acupuncture points for immediate neck pain relief. Made for lower, upper and mid chronic back pain treatment, and improves circulation, sleep, digestion and quality of life.

$$ Check price
144.87144.87Amazon 4.5 logo(1,890+ reviews)

More Back Problems resources