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
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