In Bash,nohup
and&
All can be used to run commands in the background, but their functions and uses vary. The following will introduce their characteristics, differences and applicable scenarios in detail.
1. & (background running)
&
is a special symbol of Bash that is used to put commands in the background to execute. Its main function is to allow commands to run in the background without blocking the current terminal.
- Features: Background execution: The command will be run in the background, and the user can continue to perform other operations on the current terminal.
- Dependence terminal: Although the command runs in the background, it still depends on the current terminal. If the terminal is closed (for example, the SSH connection is disconnected), the background process may be terminated.
- Output to terminal: The standard output and error output of the command are still sent to the current terminal, which may cause screen confusion.
Example:
./long_running_script.sh &
The above command will put the script in the background and run it. However, if the terminal is closed (such as disconnecting SSH), the script may be terminated.
2. nohup (ignoring suspend signals)
nohup
is a command that makes the process ignore the suspend signal (SIGHUP
), thus ensuring that the process can continue to run after the terminal is closed.nohup
The name comes from "no hang up" (not hang).
- Features: Ignore the suspend signal: Even if the terminal is closed, the process will not be terminated.
- Default output: If the output file is not specified explicitly,
nohup
Standard output and error output will be redirected to the file。
- Not necessarily running in the background:
nohup
It will not put the commands in the background, but it can be used with&
Use it in conjunction with the background operation.
Example:
nohup ./long_running_script.sh &
The above command ensures that the script can continue to run even after the terminal is closed, and the output will be redirected todocument.
3. Disown (cancel the job control)
disown
Is a command to remove background processes from job control in the current terminal. It usually has&
Use it together to ensure that the process continues to run after the terminal is closed.
Features:
- pass
disown
, the background process will no longer be controlled by the current terminal. - Once removed, terminal shutdown has no effect on the process.
Example:
./long_running_script.sh & disown
The above command first puts the script in the background and then passesdisown
Remove it from job control to ensure that the script continues to run after the terminal is closed.
Difference comparison
Function | & |
nohup |
disown |
---|---|---|---|
Running in the background | yes | Can be combined& accomplish |
Usually with& Use in conjunction |
Whether to rely on the terminal | Yes (terminal closure will terminate) | No (Ignore the suspend signal) | No (Cancel job control) |
Output redirection | No (default output to terminal) | Default redirect to (Can be changed) |
Does not affect the output |
Common scenarios | Simple backend tasks | Tasks that run for a long time and need to be kept running | The started background tasks need to be kept running |
Recommended usage
In actual scenarios, if you want the command to run in the background and continue to be executed after the terminal is closed, you can use the following command in combination:
nohup ./long_running_script.sh > 2>&1 &
explain:
-
nohup
: Ensure that the process does not terminate due to terminal shutdown. -
>
: Redirecting standard output todocument.
-
2>&1
: Redirect the error output to the same file as well. -
&
: Put the command in the background to run.
Through the above combination, background tasks can be run safely and output can be recorded in a specified file for easier subsequent analysis.
Summarize
-
&
: Suitable for simple background tasks that do not depend on terminal closure. -
nohup
: Used for long-term running tasks to avoid being affected by terminal shutdown. -
disown
: Provides additional security for tasks that are already running in the background to ensure that they are not controlled by the terminal.
Selecting suitable tools and combination methods can effectively complete tasks while ensuring the stability and operability of the system.
This is the article about the details of the difference and usage of nohup and & in Bash. This is all about this. For more information about the differences between nohup and & please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!