How to Use nohup Command in Linux?

In Linux, every command initiates a process that performs a specific task and terminates when you log out of the terminal. When the shell or terminal is exited, the process running, like; SSH Server Connection will be dropped. The nohup command prevents such situations and continues the process.

This article will assist you in the following nohup command topics:

Let’s start!

What is the nohup Command in Linux?

The nohup abbreviation of the “no hang up” command keeps the processing running after quitting the terminal/Shell. The signal used to kill or hang the process is SIGHUP (Signal Hang up) which is sent to the terminal for exiting. The nohup command stops the SIGHUP signal to keep the processes running even if you are not logged in to the terminal.

Syntax

The syntax for the “nohup” command is given below:

$ nohup [option]
$ nohup command [arguments]

The syntax components are provided below:

  • command: Replace with the specific process command.
  • arguments: Replace with the process arguments you want to initiate. 

Options

The nohup command comes with two basic options, listed below:

–helpOpen the help section for the nohup command.
–versionShows the version of the nohup present in the system.

While executing the commands the three streams are interconnected:

  • Standard Input (stdin): During the “nohup” command execution, the standard input (stdin) is not allowed to the user.
  • Standard Output (stdout): The standard output (stdout) is stored in the “nohup.out” file.
  • Standard Error (stderr): While executing the nohup command, the standard error (stderr) also uses the default file “nohup.out” to show the execution error.

Let’s dig into the usage of the nohup command.

How to Use the nohup Command in Linux?

The nohup command can start processes in the background & foreground, which are defined below:

  • Foreground Mode:A foreground process has access to the terminal (standard input and output).
  • Background Mode: This mode uses minimal or no standard input.

Let’s use the nohup command to manage the process in the foreground and background.

Run a Process in Foreground Using nohup Command

In this section, we will discuss the methods to describe running the processes with the nohup command in the foreground with different examples.

Example 1: Start a Process with nohup

We can start any process with nohup using the process command with nohup. For instance, to start a process to run the bash script from file “testscript.sh”, we used the below command:

$ nohup bash testscript.sh

Note: The “nohup: ignoring input and appending output to “nohup.out’” is not an error. It will redirect the output to the “nohup.out” file.

The above bash script saves the process output in the default nohup output file named “nohup.out”. To verify the nohup process saved, we can check the “nohup.out” file as follows:

$ cat nohup.out

The above picture displays the output for the bash script that is saved in the “nohup.out” file.

Example 2: Start a Process With nohup & Save in a Specific File

We can direct the nohup process to a specific file, i.e., “testfile.txt”. The output of the “nohup” process will be saved in that file instead of “nohup.out” file. The example command is provided below, where the output will be saved in the “tesfile.txt”:

Note: The “cat” command verifies the “nohup” process is successfully saved in the desired file:

$ nohup bash testscript.sh > testfile.txt
$ cat testfile.txt

Example 3: Save stderr to a Specific File

We can redirect the standard error (stderr) and standard output (stdout by default) file to a specific file named “testfile.txt”, use this command:

$ nohup bash testscript.sh > testfile.txt.txt >2&1

Let’s start the processes with the nohup command in the background.

Run a Process in the Background Using nohup Command

We can run the processes in the background if you want to continue executing the command after logging out of the system. The “&” symbol is used at the end of the command for running the command in the background.

Example 1: Run a Process With nohup

We can run the process in the background; for instance, to check the network connectivity with “Youtube”, use the below-mentioned command:

Note: It will keep on pinging even after logging out of the terminal.

$ nohup ping youtube.com &

The output shows the below features:

  • Column 1: [1] Job id
  • Column 2: 7607 Process ID

Example 2: Run Multiple Processes in the Background

We can run multiple processes (commands), executing the below-mentioned command. Here, the “pwd”, “ls”, and “ping” commands are used. Additionally, the cat command displays the content of the default “nohup.out” file:

$ nohup bash -c 'pwd && ls && ping www.yahoo.com' $ cat nohup.out

Example 3: Run Multiple Processes in the Background and Save them in a Specific File

We can run multiple commands/processes at a time and save them in the desired file (testfile.txt) instead of the default (nohup.out) file, using:

$ nohup bash -c 'pwd && ls' > testfile.txt $ cat testfile.txt

That’s all from this article!

Conclusion

The nohup (no hang up) command in Linux keeps running the processes even after the user is logged off the shell/terminal. This command keeps the necessary processes running in the foreground and background after exiting the terminal. Moreover, this article covered all the practical examples of the nohup command in Linux.