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
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.
To get the list of jobs either in running or stopped state you can run the below command.
$ jobs -l
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 ( ,  , 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.
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 jobs command to get the list of jobs.
$ jobs -l
From the below image you can see Job ID  is assigned to the job and
& symbol tells it is submitted and running as a background job.
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.
That’s it for this article. If there is any feedback or suggestion leave it in the comment box.