Difference between Bash Nohup and &
This short article introduces the nohup command and &
the control operator to run Linux processes in the background through Bash. In addition, we will further study &
the key differences between nohup and .
Running Linux Processes in the Background
Linux provides two ways to run processes or commands in the background.
-
Use only the ampersand (
&
) control operator. -
Use the nohup command
&
with .
The & symbol (&) control operator
We can use the & control operator to run any command asynchronously in the background.
Consider the following command.
sleep 10
sleep
command to add a delay of a specific time. When we run the sleep 10 command, it will pause the Bash terminal for 10 seconds and we cannot run any other command on the terminal.
Now, consider the following command.
sleep 10 &
The above command displays the process id (PID) and sleep 10 is executed asynchronously in the background. The execution control returns to the command terminal without waiting for the sleep to end.
Now, we can run any other command concurrently with the background sleep command on the same terminal.
We can move the background process to foreground using the following command.
fg
nohup Command
nohup
command to run any other command or process. It stands for "no hang-up" and it prevents the associated process from getting the SIGHUP signal.
If you want the command to execute even after the terminal is closed, you can use nohup CommandName.
However, if we want to run a command in the background and the execution control returns immediately to the terminal, we have to use the following command.
nohup sleep 10 &
The above command runs the sleep 10 command in the background and returns the control immediately so that we can run any other command on the same terminal.
We can use the pgrep command to view the commands running in the background, as follows:
pgrep -a [Command]
pgrep
Command Search for a command and display the process ID (PID) and details of the command being executed.
For example, pgrep -a sleep
the relevant background processes are displayed as:
PID sleep 10
Here, PID represents the process ID assigned to the sleep command.
Difference between the control operator & and the nohup command
Following are some of the differences between using &
and to run a command or process in the background.nohup
nohup can catch the hangup signal (SIGHUP), while & cannot. The SIGHUP signal is used to send a signal to a process when the terminal in which the process was started is closed.
Typically, a process or command is run in the background using & until there is a shell from which the command or process was launched. Once the shell terminates, all associated commands or processes running in the background with & will also terminate.
When the terminal exits, SIGHUP (kill SIGHUP <pid>)
the hangup signal used will terminate all subcommands or subprocesses of the terminal. However, this can be prevented using nohup.
The nohup command captures the SIGHUP signal and does not let it reach the actual command, thus limiting the command to terminate when the Bash terminal exits.
&
Another difference between and nohup
is regarding the redirection of stdout/stderr.
&
The operator does not automatically redirect stdout/stderr, but displays the command's output directly to the terminal. However, nohup redirects stdout/stderr to the file nohup.out located in $HOME.
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
Renaming Files in Bash
Publish Date:2025/03/21 Views:89 Category:OPERATING SYSTEM
-
With the help of Bash scripts, you can automate your tasks. File renaming is a common task on various systems. You can rename all the files manually. However, if your file names have a sequence, it is better to automate this task. This way
Open Emacs in Bash
Publish Date:2025/03/21 Views:141 Category:OPERATING SYSTEM
-
This article will show you how to open Emacs from within Bash. We will also discuss how to install the Emacs text editor. Install EMACS in your system Suppose you don't have Emacs in your system. You can easily install it in your system wit
Clear the Terminal Screen in Bash
Publish Date:2025/03/21 Views:145 Category:OPERATING SYSTEM
-
There are various ways to clear the terminal in bash script. This article will discuss 3 methods to clear the terminal. Use tput reset to clear the terminal screen. The first method uses the keyword tput reset to clear the screen. When your
Reload .bash_profile from the command line
Publish Date:2025/03/21 Views:67 Category:OPERATING SYSTEM
-
In the shell, .bash_profile is used to customize the configuration of user settings. It is stored in the root directory or home directory and is mostly hidden from other users. This file holds all the configuration of the shell and is also
Check if input parameter exists in Bash
Publish Date:2025/03/21 Views:191 Category:OPERATING SYSTEM
-
When we create a Bash script, we may want to use parameters in our script to run successfully. Therefore, we need to create a script to check the number of input parameters used by the user in the script. All of this prevents unexpected beh
Getting optional input arguments in Bash
Publish Date:2025/03/21 Views:180 Category:OPERATING SYSTEM
-
Sometimes we need to create a dynamic function which can be executed in both without passing any arguments or without passing any arguments. For this we need to set some default values for those arguments so that if the arguments are
Using Double and Single Pipes in Bash
Publish Date:2025/03/21 Views:136 Category:OPERATING SYSTEM
-
In Bash, the double pipe || is also known as the OR operator in other programming languages. On the other hand, the single pipe | is known as a pipeline. In this article, we will see how to use double pipe (also known as OR) and pipelines i
Breaking out of a loop in Bash
Publish Date:2025/03/21 Views:131 Category:OPERATING SYSTEM
-
Using loops is a common task in any programming or scripting language. While using a loop, sometimes we need to stop it under a predefined condition. Like other programming and scripting languages, Bash uses the keyword break to stop any lo
Bash configuration files in macOS
Publish Date:2025/03/21 Views:185 Category:OPERATING SYSTEM
-
In this article, we will discuss how to create, delete, and edit bash profiles in macOS. Create .bash_profile To create .bash_profile, open a terminal and move to your home directory using the following command. cd ~/ Now you can create usi