Tutorial: Using the Shell, Part 2

Introduction

In this tutorial, we will cover how to send programs into the background, how to restore them to the foreground, and how to send programs to sleep.

Start a program in the background

If you are going to be starting a long-running program and you want it to run in the background, you just need to add an & to the end of the command. For example, if you wanted to generate a list of all directory usage on your system, you would execute cd / then run du -h >list.txt. This could take quite a while to run on a large system. To run this in the background, use du -h >list.txt &

This will return you straight to the prompt after displaying some lines like this:

[root@plesk /]# du -h >list.txt &
[1] 1821

The number in the square brackets, in this case [1], is the job number.

Pausing a Job

If you have started a task and want to pause it, use the CTRL + z keys. This will suspend the job and put it to sleep. Use the jobs command to list all jobs. This will give you something like this:

[root@plesk /]# du -h >list.txt
^Z
[1]+  Stopped                 du -h > list.txt

As you can see, it is listed as stopped. To start it running again, use either the foreground or background commands fg or bg. For example, bg 1. If you don’t know the job number, issue the jobs command to list current jobs.

Sending a program to the background

To send a running foreground program to the background, you will need to pause it first using CTRL + z then issue the bg [jobnumber] command.

Bring a background job back to the foreground

Use the jobs command to find out what the job number is and use the fg [jobnumber] command to bring it back to running in the foreground.

Updated on February 14, 2019

Was this article helpful?

Related Articles

Need Support?
Can't find the answer you're looking for?
Contact Support