The Ops Community ⚙️

Divine Odazie
Divine Odazie

Posted on • Originally published at everythingdevops.dev

Linux background and foreground process management

This article was originally posted on Everything DevOps.

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 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.

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.

Taking a foreground job to the background with &

To start, open up your terminal window and run:

$ sleep 10000
Enter fullscreen mode Exit fullscreen mode

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.

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

$ sleep 10000 &
Enter fullscreen mode Exit fullscreen mode

After running the above command, you will get an output similar to the image below and your shell back to run other commands.

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:

$ jobs
Enter fullscreen mode Exit fullscreen mode

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

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:

$ fg %1
Enter fullscreen mode Exit fullscreen mode

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

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.

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:

$ bg %1
Enter fullscreen mode Exit fullscreen mode

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.

$ sleep 10001 &
$ sleep 10002 &
Enter fullscreen mode Exit fullscreen mode

After running the above commands, run $ jobs to see all the job processes:

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 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.

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.

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:

$ nohup sleep 10000 &
Enter fullscreen mode Exit fullscreen mode

After running the above command, you will see a terminal output similar to the image below:

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.

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.

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.

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.

To terminate the job, run:

$ pkill sleep
Enter fullscreen mode Exit fullscreen mode

After running the command, you can still see the second process there like in the image below:

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

$ sleep 10000 &
Enter fullscreen mode Exit fullscreen mode

Then disconnect it from that particular terminal session with:

$ disown %1 
Enter fullscreen mode Exit fullscreen mode

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

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:

Top comments (0)