Understanding foreground and background processes in Linux

As a Linux system administrator, you may sometimes want to run processes in the background to continue working in your command line terminal while the background process finishes its work. Linux systems allows for simultaneous process execution and ability to run programs in the foreground and background.

Running commands or processes in the background on a Linux system becomes a common task if you need to free up your terminal or disconnect from an SSH session. This is especially true of commands that run for a long time, either to listen for events or complete their lengthy task.

We have a few options on Linux for running commands in the background. In this tutorial, we’ll cover a few different methods and show you how to use them through examples. You’ll also learn how to move a process from the foreground to background and vice versa.

In this tutorial you will learn:

  • How to put a running command into the background
  • How to start a command in the background
  • How to close terminal and keep background job running
  • How to use the screen command
Understanding foreground and background processes in Linux
Understanding foreground and background processes in Linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software N/A
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Put a running command into the background

When you execute a command that wants to continue running for a long time, the problem is that your current terminal gets dedicated to the command. You can’t continue using the terminal, and you also can’t close it, because that will end the command that’s currently being run.

If you have a command running, and need to put it into the background to free up your terminal, you can press Ctrl + Z on your keyboard to stop the process. Here’s an example.

$ sleep 10000
[1]+  Stopped                 sleep 10000

To see a list of jobs in the background, use the jobs command.

$ jobs -l
[1]+  1650 Stopped                 sleep 10000

To bring a job back to the foreground, we can use the fg command.

$ fg

If we had multiple stopped jobs, we would need to use a percent sign and specify the job ID to bring the appropriate one back to the foreground.

$ fg %1

Great, now we can put processes into the background and bring them back when we want them later. But what if we want the command to continue executing while it’s in the background? For that, we can use the bg command, followed by an ampersand and the job ID. The following command will make our stopped job resume, while keeping it in the background.

$ bg %1
[1]+ sleep 10000 &

Now we can see that the process is still in the background, but it shows a status of “running” instead of “stopped.”

$ jobs -l
[1]+  1650 Running                 sleep 10000 &
Putting a process into the background and moving it to the foreground
Putting a process into the background and moving it to the foreground

There’s still one thing to keep in mind. You can’t close your current terminal, or these background jobs will close. If you need to close your terminal, and don’t want these commands to stop, then you need to “disown” the job(s). If you only have one job in the background, the following command will work:

$ disown

If you have multiple, then you’ll need to specify the job ID.

$ disown %1

You’ll no longer see the job in your jobs table when you execute the jobs command. Now it’s safe to close the terminal and your command will continue running.

$ jobs -l

You can still keep an eye on your running command by using the ps command.

$ ps aux | grep sleep
linuxco+    1650  0.0  0.0   8084   524 pts/0    S    12:27   0:00 sleep 10000

And if you want to stop the command from running, you can use the kill command and specify the process ID.

$ kill 1650

Start a command in the background

Now we have seen how to put a running process into the background and recall it later. But we also have the option to just start the command in the background at the beginning. All you need to do is put an ampersand at the end of any Linux command.

$ sleep 10000 &
[1] 1900

Just as before, we can see the job listed with the jobs command. This time, though, the job is already running and we didn’t have to manually start it in the background.

$ jobs -l
[1]+  1900 Running                 sleep 10000 &

Close terminal and keep background job running

We’ve already seen in a previous example how we can use the disown command to ensure that the command continues running after we close our terminal. Another option is the nohup command.

nohup tells a process to ignore any SIGHUP (hangup) signals that it receives. The SIGHUP signal is sent to a background job whenever the current terminal is closed. Thus, we can have a job in the background, close the terminal, and still see it running in the background. Here’s an example.

$ nohup sleep 10000 &
[1] 1908
nohup: ignoring input and appending output to 'nohup.out'

We have closed the terminal and reopened it, then ran the following command, which shows the command is still running.

$ ps aux | grep sleep
linuxco+    1908  0.0  0.0   8084   524 ?        S    13:05   0:00 sleep 10000

Using the screen command

Up until now, we’ve covered the default Linux methods for running commands in the background. However, there’s also extra software packages that can be used. Perhaps the most popular would be the screen command.

You can see the aforementioned link for installation instructions and detailed usage, but the gist of it goes like this:

  1. Run the screen command to start a new “screen”. Optionally, include the -S option to give it a name.
    $ screen -S mycommand
  2. In the new screen session, execute the command or script you wish to put in the background.
    $ /path/to/myscript.sh
  3. Press Ctrl + A on your keyboard, and then D. This will detach the screen, then you can close the terminal, logout of your SSH session, etc, and the screen will persist. To see a list of screens, use this command.
    $ screen -ls
    There is a screen on:
    	2741.mycommand	(04/08/2021 01:13:24 PM)	(Detached)
    1 Socket in /run/screen/S-linuxconfig.
  4. To reattach to a screen, use the following command, substituting the number below with the process ID of your own.
    $ screen -r 2741

Closing Thoughts

In this tutorial, we saw several different methods for running a command in the background on Linux. This included default Linux utilities and the screen command. We also saw how to manage background process by moving jobs into the foreground, reattaching screens, and killing background processes.

Comments and Discussions
Linux Forum