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
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u