How to Use Linux nohup Command

December 16, 2021

Introduction

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.

Using the 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.

nohup Syntax

The syntax for using the nohup command is:

nohup [command] [argument]

nohup Options

The nohup command features two basic command options, the --help and the --version options.

To display the help message, run the command:

nohup --help

To display version information, type:

nohup --version

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.

nohup Examples

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 nohup command.

1. Running 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 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:

cat nohup.out

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 - [1] 7366 in the example below.

To bring the command in the foreground, type:

fg

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

Replace [command1] and [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:

cat nohup.out

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:

cat output.txt

Conclusion

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.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.
Next you should read
How to Use the top Command in Linux
December 2, 2021

The top command is a built-in Linux utility that contains useful information about your...
Read more
How To Use shred Linux Command
March 31, 2021

Deleting a file in Linux or any other OS does not actually remove the data from the drive...
Read more
How to Use the Linux cd Command to Change Directory
December 8, 2021

The cd command in Linux allows you to change the current working directory using...
Read more
Linux set Command & How to Use it {9 Examples}
November 23, 2021

The set command allows you to show or change the shell and environment variables...
Read more
  • © 2022 Copyright phoenixNAP | Global IT Services. All Rights Reserved.