SIGHUP (Signal Hang UP) is a signal that terminates a Linux process when its control terminal is closed. If you accidentally close a terminal or lose connection to the server, all processes running at that time are automatically terminated.
Using the nohup command is
a way to block the SIGHUP signal and allow processes to complete even after logging out of the terminal/shell. In this article, you will learn how to use the nohup command while running processes. nohup syntax The syntax for using the nohup command is: nohup [command] [argument] nohup options The
nohup command presents two basic command options, the –help and -version options.
To display the help
message, run the command
: nohup -help To display the version information,
nohup -version nohup Examples
There are several 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 following examples explain common use cases for the nosup command
1. Run a process with
nohup To run a command using nohup
without any arguments, simply follow the syntax: nohup [command] The shell ignores the output and appends it to the nohup.out file. For example, running the bash example.sh script (which is a simple Hello World script) should display the Hello World message in the nohup.out
: nohup bash file example.sh
Verify the contents of the file with:
Running a background process with nohup
Running a Linux process in the background frees up the terminal you’re working on. To run a Linux process in the background with the nohup command, add the & symbol to the end of the command: nohup [command] & For example, to run the bash script example.sh in the background, use the command: nohup bash example.sh & The output shows the shell job ID and process ID –  7366 in the example below. To bring the command to the
The result indicates whether the process is ongoing or complete
3. Running multiple processes in the background
with nohup nohup bach -c ‘[command1] && [command2]’
Replace [command1] and [command2] with the commands of your choice. Add more commands if needed, making sure to separate them with &&.
For example, to display the date/time and calendar of the
current month, run
: nohup bash -c ‘date && cal’ As the output goes to the nohup.out file, use the cat command
to list the contents of the file and check the above command: cat nohup.out
The output displays the date and calendar requested with the above command
the output to a different file As mentioned in the previous section, nohup
logs all output messages and errors in the nohub.out file. Redirect these messages by specifying a custom location within the command: nohup [command] > /path/to/output/file.txt In the following example, the output of the nohup bash -c command ‘date && cal’ is redirected to the output file
.txt. Check the output with the command:
After reading this article, you should know how to use the nohup command to run background processes and redirect their output
As an alternative to using the nosup command, consider checking out Tmux. Tmux was built to support multitasking in a terminal window. Learn more in our comprehensive Tmux tutorial.