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 to the terminal immediately, 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
Run a batch (.bat) file in CMD
Publish Date:2025/04/21 Views:169 Category:OPERATING SYSTEM
-
This article will show you how to use CMD to run a batch file.bat. There are three ways in which you can run a batch file. Let us discuss them in the following sections. Run batch (.bat) files in CMD by directly clicking on them This way yo
Running batch scripts using Task Scheduler
Publish Date:2025/04/21 Views:188 Category:OPERATING SYSTEM
-
This article will show you how to use Task Scheduler to run a batch file. Running batch scripts using Task Scheduler With Task Scheduler, you can automate tasks to run automatically at specific times. It only takes a few steps and you don't
Solve the error Make Command Not Found in Cygwin
Publish Date:2025/04/21 Views:75 Category:OPERATING SYSTEM
-
Cygwin allows Windows users to access certain Linux features and includes a large number of GNU and open source tools that are commonly found in popular Linux distributions. When using Cygwin, it is easy to encounter a command not found err
Pull all branches in Git
Publish Date:2025/04/21 Views:98 Category:OPERATING SYSTEM
-
Git provides us a platform where we can maintain multiple separate development commits for a new project called branches. We can restore the latest version of a branch from a remote repository as needed or we can restore all branches at onc
Git list remote branches
Publish Date:2025/04/20 Views:55 Category:OPERATING SYSTEM
-
This article will show you how to list remote repositories from your local branch. A remote repository is a project hosted on a server, such as Github/Gitlab. git remote Allows us to use short names (aliases) to execute commands instead of
Pushing from an existing remote repository to another remote repository in Git
Publish Date:2025/04/20 Views:68 Category:OPERATING SYSTEM
-
This tutorial will teach you how to push from an existing remote repository to a different remote repository in Git. Git is a version control system used to track changes in a project directory. Git uses commits for such purposes. In Git, y
Update the repository remotely by setting
Publish Date:2025/04/20 Views:120 Category:OPERATING SYSTEM
-
In this tutorial, we will discuss how to set up the central repository as a remote for our local repository so that our branch is updated whenever the central repository changes. We should always perform this step before making edits to our
Rename Git repository
Publish Date:2025/04/20 Views:100 Category:OPERATING SYSTEM
-
In this article, we will discuss renaming Git repositories. We can explain this in different ways. It can rename the displayed name, the repository on GitHub, or the folder of the repository. We will discuss these and go through the steps w
Creating tags in a Git repository
Publish Date:2025/04/20 Views:118 Category:OPERATING SYSTEM
-
In this tutorial, we will discuss how to create tags in a Git repository. Creating tags in a Git repository In Git, we may want to mark certain commits or specific points in the history of the project repository. To do this, we can use the