Home Linux Commands How to Work with Foreground and Background Process in Linux

How to Work with Foreground and Background Process in Linux

An important concept to understand when working with the Linux process is what is the foreground and background process and how to control them. In Linux, if you execute any programs a process will be created with a unique ID (PID) and by default, the process runs in the foreground.

Let’s take a simple curl command – when you send a request to download a zip file over the internet, curl will run as a foreground process and all the outputs will be displayed in the terminal.

There are two important keystrokes that you have to understand before working with the background and foreground process.

  • CTRL+Z – This keystroke will stop the running process.
  • CTRL+C – This keystroke will kill the running process and free up memory in RAM.

Linux Foreground Process

A process when you start from the terminal by default runs as a foreground process. The foreground process will not allow you to use the terminal unless the process is completed. In this case, if you need access back to your terminal then you have to open a new terminal window or stop the running process or kill it.

It is also important to note, any process that is created via the terminal is attached to the terminal session and the process will be terminated once the terminal is closed. This is because bash is the parent process and any command you run in the terminal will be the child process to the bash so when you close the terminal parent process (bash) will automatically terminate all its child processes and its own.

Below is an example of the foreground process. This is just a simple sleep command that will return the prompt to the user only after it is completed.

$ sleep 10000
Linux Sleep Command
Linux Sleep Command

Now I have few options to gain control back to the terminal prompt. Either I have to cancel or stop the process or open a new terminal tab. When you stop a running process by pressing (CTRL+Z) you will get output as shown in the below image.

Stop Running Linux Process
Stop Running Linux Process

To get the list of jobs either in running or stopped state you can run the below command.

$ jobs -l
List Running Linux Jobs
List Running Linux Jobs

From the above image, you can see the jobs command gives you the process ID, what command you submitted, what is the status of it. You will also get a job ID for every process that you submit ( [1], [2] [3], etc..).

To start a job that is in the stopped state or bring the background job to the foreground run the following command.

$ fg %4     

Where %4 denotes the job ID that you can get from the “jobs -l” command.

Start Job in Foreground
Start Job in Foreground

Linux Background Process

Background process runs your process in the background and will not take control of your terminal prompt. You can start a session and you are free to use your terminal. To submit a command as a background process you have to add & symbol at the end of the command.

$ sleep 50000 &
Run Linux Command in Background
Run Linux Command in Background

Run jobs command to get the list of jobs.

$ jobs -l

From the below image you can see Job ID [5] is assigned to the job and & symbol tells it is submitted and running as a background job.

List Running Linux Comamnds (Jobs)
List Running Linux Commands (Jobs)

You can start any stopped job in the background directly by running the following command.

$ bg %2 

Where %2 denotes the job ID that you can get from the “jobs -l” command.

Run Linux Command in Background
Run Linux Command in Background

That’s it for this article. If there is any feedback or suggestion leave it in the comment box.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.