Running Linux Commands in Background and Foreground

If you have a long-running task, it’s not always wise to wait for it to finish. I mean why keep the terminal occupied for a particular command? In Linux, you can send a command or process to the background so that the command would be running but the terminal will be free for you to run other commands.

In this tutorial, I’ll show you a couple of ways to send a process in the background. I’ll also show you how to bring the background processes back to the foreground.

Start a Linux process in the background directly

If you know that the command or process is going to take a long time, it would be a better idea to start the command in the background itself.

To run a Linux command in the background, all you have to do is to add an ampersand (&) at the end of the command, like this:

your_command &

Let’s take a simple bash sleep command and send it to the background.

sleep 60 &

When the command finishes in the background, you should see information about that on the terminal.

[1]+  Done                    sleep 60

Send a running Linux process to the background

If you already ran a program and then realized that you should have run it in the background, don’t worry. You can send a running process to the background as well.

What you have to do here is to use Ctrl+Z to suspend the running process and then use ‘bg‘ (short for background) to send the process in the background. The suspended process will now run in the background.

running_command
^z
bg

Let’s take the same example as before.

abhishek@linuxhandbook:~$ sleep 60
^Z
[1]+  Stopped                 sleep 60
abhishek@linuxhandbook:~$ bg
[1]+ sleep 60 &

See all processes running in the background

Now that you know how to send the processes in the background, you might be interested in knowing which commands are running in the background.

For this purpose, you can enter this command in the terminal:

jobs

Let’s put some commands in the background first.

firefox &
gedit &
vim &

Now the jobs command will show you all the running jobs/processes/commands in the background like this:

jobs
[1]   Running                 firefox &
[2]-  Running                 gedit &
[3]+  Stopped                 vim

Do you notice the numbers [1], [2] and [3] etc? These are the job ids. You would also notice the – and + sign on two of the commands. The + sign indicates the last job you have run or foregrounded. The – sign indicates the second last job that you ran or foregrounded.

Bring a Process to Foreground in Linux

Alright! So you learned to run commands in the background in Linux. But what about bringing a process running in the background to the foreground again?

To send the command to the background, you used ‘bg’. To bring the background process back, use the command ‘fg’.

fg

Now if you simply use fg, it will bring the last process in the background job queue to the foreground. In our previous example, running ‘fg’ will bring Vim editor back to the terminal.

If you want to bring a certain process to the foreground, you need to specify its job id. The job id is the number you see at the beginning of each line in the output of the ‘jobs’ command.

fg n

Where n is the job id as displayed in the output of the command jobs.

That’s it

This was a quick one but enough for you to learn a few things about running commands in the background in Linux. I would advise learning nohup command as well. This command lets you run commands in the background even after you log out of the session.

If you have questions or suggestions, please leave a comment below.