How to Run a Background Process in Bash

MD Aminul Islam Feb 02, 2024
  1. Create a Background Process Using & in Bash
  2. Kill a Background Process in Bash
  3. Keep the Current Job Running Using disown in Bash
  4. Prevent the Background Process to Continue Printing Messages Into the Terminal
  5. Keep a Command Running in the Background Using nohup
  6. Convert a Running Foreground Process to a Background Process in Bash
How to Run a Background Process in Bash

When executing a command in the terminal, you need to wait until the command finishes its execution. This is called the foreground process.

However, some advanced programs need to be run in the background. In a Bash script, there is an easy way to make your command run in the background.

The interesting thing about a background process is that you need not wait for the end of executing a command. You can run another command parallelly.

But you cannot end a background process by only clicking the cross button. You have to use some commands to end a background process.

In this article, we will discuss how we can create a command that runs in the background. Also, we will discuss the topic by using necessary examples and explanations to make the topic easier.

We will first learn how to create a background process and then see how to kill an existing process.

Create a Background Process Using & in Bash

Creating a background process is very easy in Bash script. All you need to do is to include the ampersand symbol & at the end of the command.

The general syntax for making a command run in the background is:

YOUR COMMAND &

The example below will run a Bash Script as a background process. The code will look like the following:

./example.sh &

After executing the above code, you will obtain this output:

[1] 20

Here, you will see the process number surrounded by the third bracket.

If your process has done its job, you will get notified like below:

[1]+  Done                    ./example.sh

Kill a Background Process in Bash

To kill a background process, you only need to do the following steps. If you forgot the process ID, you can find your running process using the command below.

jobs -l

This will output the list of the running processes.

[1]+ 25177 Running                 ./example.sh &

From the list, you can find the process ID and use this to kill the process using the following command.

kill %1

The general syntax for killing a process is:

kill %ProcessID

Keep the Current Job Running Using disown in Bash

You can keep the current job running in the background even if the terminal is closed. For this purpose, we will utilize a command named disown.

To keep a current job running in the background constantly, you can follow the below example command:

disown -h %3

We keep the process with id 3 alive even if the terminal is closed. Here, -h is an option to keep the process alive.

The general syntax for this command is:

disown -h %ProcessID

Prevent the Background Process to Continue Printing Messages Into the Terminal

To prevent a background process from printing massages into the terminal window, you can follow the below syntax:

Your_Command 2>/dev/null &

This will stop printing messages in the terminal window.

Keep a Command Running in the Background Using nohup

If you want to keep a process running in the background, even if the terminal is closed, you must use the nohup command. This command will execute other programs specified as its argument and ignores all the SIGHUP signals.

When you close the terminal, it sends a SIGHUP signal to close all the running processes under the controlling terminal. As this command ignores the SIGHUP command, the process run by the nohup command is always alive until it is killed.

You have to include the & with this command. You can follow the example below:

nohup ./MyScript.sh &

This command will run the background process and write its output to a nohup.out file.

Convert a Running Foreground Process to a Background Process in Bash

To transfer a foreground process to the background, you have to follow the two steps below:

  1. First, you have to stop the targeted process by pressing Ctrl+Z.
  2. Move the stopped process to the background using the command bg. You can follow the below syntax:
bg %ProcessID

All the codes used in this article are written in Bash. It will only work in the Linux Shell environment.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn

Related Article - Bash Background