Brief: This tutorial shows how to run process in background in Linux and how to bring back background processes to 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 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 background. I’ll also show you how to bring the background processes back to foreground.
Start a Linux process in 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 background itself.
To run a Linux command in background, all you have to do is to add ampersand (&) at the end of the command, like this:
Let’s take a simple bash sleep command and send it to background.
sleep 60 &
When the command finishes in the background, you should see an information about that on the terminal.
+ Done sleep 60
Send a running Linux process to background
If you already ran a program and then realized that you should have run it in background, don’t worry. You can send a running process to 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 background. The suspended process will now run in background.
Let’s take the same example as before.
abhishek@linuxhandbook:~$ sleep 60
+ Stopped sleep 60
+ sleep 60 &
See all processes running in background
Now that you know how to send the processes in 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:
Let’s put some commands in the background first.
Now the jobs command will show you all the running jobs/processes/commands in the background like this:
 Running firefox &
- Running gedit &
+ Stopped vim
Do you notice the numbers ,  and  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 background in Linux. But what about bringing a process running in the background to foreground again?
To send the command to background, you used ‘bg’. To bring background process back, use the command ‘fg’.
Now if you simply use fg, it will bring the last process in the background job queue to 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 in the beginning of each line in the output of the ‘jobs’ command.
Where n is the job id as displayed in the output of the command jobs.
This was a quick one but enough for you to learn a few things about running commands in background in Linux. I would advise learning nohup command as well. This command lets you run commands in background even after you log out of the session.
If you have questions or suggestions, please leave a comment below.