NOHUP stands for No Hang-up, a command in Linux that simply ignores the Hang-up signal. It means that running a command while using the NOHUP command will keep it running even if the user logs out of the system. However, once this command finishes its execution, its output will be saved in a file named “nohup.out” instead of appearing on the terminal. In this guide, we will learn a little more about the NOHUP command in Linux.
NOHUP Command in Linux
The usage of the NOHUP command in Linux can quickly be learned by looking at its help manual. To access that, you will have to execute the following command:
$ nohup --help
You can see the help manual for this command in the following image:
However, to use this command effectively, you can go through the following examples:
Example 1: Run a Command With NOHUP
In this example, we will use the NOHUP command in Linux to run a bash script. The exact command for this purpose is as follows:
$ nohup bash Space.sh
Here, “Space.sh” is the name of the bash script file that needs to be executed.
Since we have used the NOHUP command for executing this script, its output will not appear on the terminal. Instead, it will be redirected to a newly created file named “nohup.out”, as shown in the following image:
Example 2: Redirect the Output of a Command to a Different File With NOHUP
In the previous example, we did not provide a name for the file in which the output of our command is supposed to be redirected. However, in this example, we will be using the NOHUP command for redirecting the output of another command to a specifically mentioned file, as you can witness from the following command:
$ nohup bash Space.sh > newfile.txt
You can have any name of your choice for the output file. However, in this particular case, the output of our bash script will be redirected to the “newfile.txt” file and not to the “nohup.out” file, as shown in the following image:
To confirm if the output of the bash script has been redirected to the said output file or not, we will display the contents of this file with the following command:
$ cat newfile.txt
The contents of this file shown in the following image confirm that the output of our bash script has successfully been redirected to this file.
Example 3: Run a Command in the Background With NOHUP
Now, we will use the NOHUP command to run another command in the background in Linux. It means that even after closing the terminal, that command will keep running in the background until we explicitly kill it. To understand this, you can consider the following command:
$ sudo nohup ping &
The previous command will start executing the ping command in the background, as shown in the following image:
Now, even if you close your terminal, this command will keep running in the background. Therefore, whenever you wish to terminate its execution, you will have to kill it explicitly with the following command:
$ kill -9 2700
Here, “2700” refers to the process ID (PID) of the command to be killed that appeared earlier due to executing the NOHUP command.
Conclusion
This article introduced you to the NOHUP command in Linux. This command is handy when a user cannot stay logged in all the time but still wants their work done in their absence. With the help of a few relevant examples, we have tried to convey the basic gist of this Linux command. By now, you will have an excellent idea of how the NOHUP command works in Linux.