Linux background and foreground process management
Tags: linux, management, devops, codenewbie
Published: 2022-05-20 16:09:04

This article was originally posted on [Everything DevOps](https://everythingdevops.dev/linux-background-and-foreground-process-management).

There is support for background and foreground job processing in Linux-based operating systems. A job in this context is just a command launched from a terminal window. Any running command is a process. 

This tutorial will show you how to manage jobs in the foreground and background of your Linux terminal window. In the end, you would've learned: 


- Why you would manage jobs
- How to take a foreground job to the background with `&`
- How to bring a background job to the foreground with `fg`
- How to suspend a foreground job with `CTRL + Z`
- How to resume a suspended foreground job with `bg`
- The meaning of the `+` and `-` symbol on background jobs
- How to run job processes even after closing the terminal window

## Why manage jobs?

By default, all jobs in Linux execute in the foreground. Foreground jobs run directly from the shell. When you run one foreground job, you will have to wait for shell access until the job completes before you can run other jobs.

Waiting for jobs to complete is fine when the jobs complete quickly. But in cases where the current job is going to take a long time (even several hours) to complete, it becomes a challenge, especially when you have a single terminal window ([SSH](https://en.wikipedia.org/wiki/Secure_Shell) session, wconsole) to work with.

In such cases, you can run the job in the background and free the shell for other jobs and tasks. When you run a Job in the background, it will execute at low priority, which will, in turn, allow typing of other commands in the terminal window while the job runs.

Next, you will see how to manage jobs in the foreground and background.

## Prerequisite

To follow along on managing jobs in foreground and background in Linux, you need access to a Linux distribution terminal window. 

This tutorial uses an [Ubuntu Linux distribution](https://en.wikipedia.org/wiki/Ubuntu).

# Managing jobs in the foreground and background

This tutorial will use the `sleep` command to create dummy jobs for illustration purposes. The `sleep` command, when run, delays the shell for a specified timeframe. To learn more about the `sleep` command, check out this [article](https://www.geeksforgeeks.org/sleep-command-in-linux-with-examples/). 

## Taking a foreground job to the background with `&`

To start, open up your terminal window and run:

```bash
$ sleep 10000
```
After running the above command, it delays the shell for `10000` seconds, making  it impossible to run any other command until after `10000` seconds, as shown in the image below. 

![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651517716461_Screenshot+from+2022-05-02+19-54-54.png)


To run the above `sleep` command in the background, after terminating the currently running job with `CTRL + C`, run:

```bash
$ sleep 10000 &
```
After running the above command, you will get an output similar to the image below and your shell back to run other commands. 

![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651517758791_Screenshot+from+2022-05-02+19-55-33.png)


In the above image, the `sleep 10000 &` is process `3647` job number `1`, and it is currently running in the background.

To see the Job, run:

```bash
$ jobs
```
The above command will show all the jobs in the background of your terminal session and their current state. 

![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651517791193_Screenshot+from+2022-05-02+19-56-14.png)

## Bringing a background job to the foreground with `fg`

In some cases, after taking a job to the background, you might want to do something with it; that’s where the `fg` command comes in. You use the `fg` to move a background job on your current Linux shell to the foreground. 

To bring back your current running job to the foreground, run:

```bash
$ fg %1
```
Next to `%` in the above command is the job number `1`. After running the command, you will see it back in the foreground.

![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651517824865_Screenshot+from+2022-05-02+19-56-50.png)

## Suspending a foreground job with `CTRL + Z`

Let’s say you want to suspend the job for a while to run other commands; you can do that with `CTRL + Z`. `CTRL + Z` puts the job in a “stopped” mode and doesn’t terminate it. 


![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651517864973_Screenshot+from+2022-05-02+19-57-26.png)

## Resuming a suspended foreground job with `bg`

To resume the `sleep 10000` process, you can use the `bg` command. `bg` will resume the job process and take it to the background. 

In the shell, run:

```bash
$ bg %1
```

![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651517910656_Screenshot+from+2022-05-02+19-58-15.png)

## Meaning of the `+` and `-` symbol on background job processes

If you’ve noticed throughout the screenshots, the `+` symbol beside the job number, well, there’s also `-` and they both have meanings, as you can see in the quote below:


> In output pertaining to jobs command), the current job is always flagged with a `+`, and the previous job with a `-`. — **"JOB CONTROL" section on the bash manpage.**

 To show the above quote, create two more dummy jobs in the background.

```bash
$ sleep 10001 &
$ sleep 10002 &
```
After running the above commands, run `$ jobs` to see all the job processes:

![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651517621964_Screenshot+from+2022-05-02+19-53-21.png)


As you can see in the image above, the most recent just you created is flagged with the `+` symbol and the previous with `-`.

So far, you’ve learned how to take foreground jobs to the background, bring them back to the foreground, suspend them and resume them. Doing all this helps you be more efficient when you have a single terminal window to work. 

Next, you will learn how to make your job run even after closing the terminal window.

## Running jobs even after closing the terminal window

When you have jobs running in the background or foreground, all the jobs will be terminated if you close the terminal window. The job termination is because they are associated with that particular terminal session. And upon exit, a [hang-up (HUP) signal](https://www.computerhope.com/unix/signals.htm) is sent to all the processes it started.

To practicalize this, if the previous jobs you created are still running, close the terminal window, open it back up and then run `$ jobs`. You will see that no job process is running like in the image below.

![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651507875777_Screenshot+from+2022-05-02+17-10-53.png)


To make jobs continue running even after closing the terminal window, you can use `nohup`, `disown`, and other commands. This tutorial uses only `nohup` and `disown`. To learn about the other commands, check out these [recommendations by other Linux users on askubuntu](https://askubuntu.com/questions/8653/how-to-keep-processes-running-after-ending-ssh-session). 

**When to use** `nohup`? **When to use** `disown`?
If you’re able to think ahead of time that you will close your terminal window and not want to hang up your running jobs, use `nohup`. If not, use `disown`, which allows you to disconnect a process from a terminal session after its already been created.

**Using** `nohup`
To use `nohup` and continue running jobs even after closing the terminal window, when creating the job, add `nohup`. 

To practicalize, in your terminal window, create a dummy job with `nohup`:

```bash
$ nohup sleep 10000 &
```
After running the above command, you will see a terminal output similar to the image below:

![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651511844156_annotely_image+27.png)


The annotated terminal output above means that `nohup` will append (add) any output from the job process `sleep 10000` to a file called `nohup.out`. You can find the `nohup.out` file in your present working directory, as you can see in the image below.


![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651513553797_annotely_image+28.png)


You can also redirect a particular job output to a different file. To learn how to do that, check out this [question and answer on Unix & Linux StackExchange](https://unix.stackexchange.com/questions/45913/is-there-a-way-to-redirect-nohup-output-to-a-log-file-other-than-nohup-out).

Now, when you close your terminal and open it back up, you can still find your `sleep 10000` job running. But this time, not with the `$ jobs` command but with `$ ps -aux | grep sleep` commands, as you can see in the image below. 


![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651514098328_annotely_image+29.png)


The second `sleep` process you see in the above terminal output was not started by you but by the `grep` command. To learn why it is there, check out these answers on [stackoverflow](https://stackoverflow.com/questions/6893714/why-does-ps-o-p-list-the-grep-process-after-the-pipe).

To terminate the job, run:
```bash
$ pkill sleep
```
After running the command, you can still see the second process there like in the image below:

![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651514770538_Screenshot+from+2022-05-02+19-05-56.png)


**Using** `disown`
Since all the jobs you’ve created are “stopped,” to learn about how to use `disown`, create a new dummy job process:

```bash
$ sleep 10000 &
```
Then disconnect it from that particular terminal session with:

```bash
$ disown %1 
```

![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651517509279_Screenshot+from+2022-05-02+19-51-27.png)


Now, close the terminal window, open it up again, then run `$ ps -aux | grep sleep` to see the job process running like in the image below 

![](https://paper-attachments.dropbox.com/s_074FDC3BA30B24BE29F6F42415590A6BBC8A4F87A9FF7F91F2F015E14DBF952A_1651517453412_annotely_image+30.png)

## Conclusion

This tutorial taught you how to manage foreground and background job processes in a Linux terminal window. To learn more about managing processes in Linux, check out the following resources:


- [Linux Internals - Process Management](https://www.youtube.com/watch?v=Ip9P1XJt5PI)
- [How to use the Linux Screen program](https://youtu.be/VC50SpHlkz8)