Running Background Processes in Bash
When you execute a command in the terminal, you need to wait for the command to finish executing. This is called the foreground process.
However, some advanced programs need to run in the background. In Bash scripts, there is an easy way to have your commands run in the background.
The interesting thing about background processes is that you don't need to wait for the command execution to finish. You can run another command in parallel.
But you can't end the background process just by clicking the cross button. You have to use some command to end the background process.
In this article, we will discuss how to create a command that runs in the background. Furthermore, we will discuss the topic by using necessary examples and explanations to make the topic easier to understand.
We will first learn how to create a background process and then see how to terminate an existing process.
Creating Background Processes Using & in Bash
Creating a background process in a Bash script is very easy. All you need to do is include the & symbol at the end of the command.
The general syntax for making a command run in the background is:
YOUR COMMAND &
The following example runs a Bash script as a background process. The code will look like this:
./example.sh &
After executing the above code, you will get the following output:
[1] 20
Here you will see the process number enclosed in the third bracket.
If your process has completed its work, you will be notified like this:
[1]+ Done ./example.sh
Killing Background Processes in Bash
To kill a background process, just follow these steps. If you forget the process ID, you can use the command below to find your running process.
jobs -l
This will output a list of running processes.
[1]+ 25177 Running ./example.sh &
From the list, you can find the process ID and use it 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 also keep the current job running in the background even when the terminal is closed. To do this, we will use a command called disown.
To keep the current job running in the background, you can follow this example command:
disown -h %3
We will keep the process with id 3 alive even if the terminal is closed. Here, -h is the option to keep the process alive.
The general syntax of this command is:
disown -h %ProcessID
Stop the background process from continuing to print messages to the terminal
To prevent a background process from printing messages to the terminal window, you can follow this syntax:
Your_Command 2>/dev/null &
This will stop printing messages in the terminal window.
Use nohup to run the command in the background
If you want a process to run in the background even if the terminal is closed, you must use the nohup command. This command will execute other programs specified as its parameters and ignore all SIGHUP signals.
When you close the terminal, it sends a SIGHUP signal to close all running processes under the controlling terminal. Since this command ignores the SIGHUP command, the process run by the nohup command remains active until it is killed.
You must include the & in this command. You can follow this example:
nohup ./MyScript.sh &
This command runs a background process and writes its output to the nohup.out file.
Convert a running foreground process to a background process in Bash
To move a foreground process to the background, you must perform the following two steps:
- First, you must stop the target process by pressing Ctrl+Z.
- Use the command bg to move the stopped process to the background. You can follow the syntax:
bg %ProcessID
All the codes used in this article are written in Bash. It will only work in Linux Shell environment.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Hosting Docker Internal in Linux
Publish Date:2025/03/23 Views:143 Category:OPERATING SYSTEM
-
Docker allows developers to efficiently build, test, and deploy applications by packaging them in standardized units called containers. When working with Docker containers, you may encounter scenarios where you need to connect a container t
Setting the working directory in Docker
Publish Date:2025/03/23 Views:198 Category:OPERATING SYSTEM
-
If present, the working directory of a process in Compute is a directory in a linked hierarchical file system that is dynamic for each process. In Docker, we can set our working directory by editing the Dockerfile and adding the key WORKDIR
How to get IP address in CentOS
Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM
-
This short article is a brief introduction to CentOS followed by a brief discussion on how we can get the server IP address in CentOS using the Command Line Interface (CLI). This article will discuss some of the commands and their usage for
Updating YUM in Linux
Publish Date:2025/03/23 Views:148 Category:OPERATING SYSTEM
-
This article will teach us how to update YUM in Linux and how to install, update, remove, find and manage packages on a Linux system. We have also seen the difference yum update between and in Linux yum upgrade . yum update command in Linux
Installing Deb Files in Linux
Publish Date:2025/03/23 Views:93 Category:OPERATING SYSTEM
-
In this Linux article, we will learn how to install .deb (Debian Package) files on Linux systems. We will also see how to remove .deb files after installation. More importantly, we will learn different ways to install .deb files on Linux sy
lsof Command in Linux
Publish Date:2025/03/23 Views:100 Category:OPERATING SYSTEM
-
In this Linux article, we will learn about lsof command in Linux operating system. We will see how to use this command for different purposes in Linux. We use lsof the lsof command to verify the ports in use on the Linux operating system. U
How to solve the problem of not being able to execute binary files in Linux
Publish Date:2025/03/23 Views:108 Category:OPERATING SYSTEM
-
In this article, we will learn how to execute binary files in Linux. We will also learn how to troubleshoot the error if Linux fails to execute the binary file. Usually, this error occurs when we run shell scripts in Linux. This article wil
Error in Linux Mesg: Ttyname Failed: Inappropriate Ioctl for Device Error
Publish Date:2025/03/23 Views:178 Category:OPERATING SYSTEM
-
In this article, we will learn how to fix the error mesg: ttyname failed: Inappropriate ioctl for device in Linux . We will discuss some of the causes of this error and show you how to fix it. Let's start with what causes this error in Linu
ps aux command in Linux
Publish Date:2025/03/23 Views:57 Category:OPERATING SYSTEM
-
If you are using Linux and are looking for a tool that can monitor all the processes running on your system, then you should use the command ps aux. This command will show you an overview of all running processes. It is very useful for trou