SIGHUP (Signal Hang UP) is a signal that terminates a Linux process when its controlling terminal is closed. If you accidentally close a terminal or lose connection with the server, all processes running at the time are automatically terminated.
nohup command is one way of blocking the
SIGHUP signal and allowing processes to complete even after logging out from the terminal/shell.
In this article, you will learn how to use the
nohup command while running processes.
The syntax for using the
nohup command is:
nohup [command] [argument]
nohup command features two basic command options, the
--help and the
To display the help message, run the command:
To display version information, type:
Note: Although nohup may appear similar to daemons, there is a significant difference. Daemons are reserved for processes that continuously run in the background, while nohup is used for processes that take a long time but don't continue running once done.
There are a number of ways to use the
nohup command, including running the required process in the background, running multiple processes simultaneously, or redirecting the output to a different file.
The examples below explain common use cases for the
1. Running a Process with nohup
To run a command using
nohup without any arguments, simply follow the syntax:
The shell ignores the output and appends it to the nohup.out file.
For instance, running the example.sh bash script (which is a simple Hello World script) should prompt the Hello World message in the nohup.out file:
nohup bash example.sh
Verify the contents of the file with:
2. Running a Process in the Background with nohup
Running a Linux process in the background frees up the terminal you are working in. To run a Linux process in the background with the
nohup command, add the
& symbol at the end of the command:
nohup [command] &
For example, to run the example.sh bash script in the background, use the command:
nohup bash example.sh &
The output displays the shell job ID and process ID -  7366 in the example below.
To bring the command in the foreground, type:
The output indicates whether the process is in progress or complete.
Note: For a list of all the important Linux commands in one place, check out our Linux Commands Cheat Sheet.
3. Running Multiple Processes in the Background with nohup
nohup bach -c '[command1] && [command2]'
[command2] with the commands of your choice. Add more commands if necessary, making sure to separate them with
For instance, to show the date/time and the calendar of the current month, run:
nohup bash -c 'date && cal'
As the output is directed to the nohup.out file, use the cat command to list the contents of the file and verify the command above:
The output shows the date and calendar prompted with the command above.
4. Redirecting Output to a Different File
As mentioned in the section above,
nohup logs all output messages and errors into the nohub.out file.
Redirect these messages by specifying a custom location within the command:
nohup [command] > /path/to/output/file.txt
In the example below, the output for the command
nohup bash -c 'date && cal' is redirected to the output.txt file. Check the output with the command:
After reading this article, you should know how to use the
nohup command to run processes in the background and redirect their output.
As an alternative to using the
nohup command, consider checking out Tmux. Tmux was built to support multitasking in a terminal window. Learn more in our comprehensive Tmux Tutorial.