nohup Command in Linux

Learn via video courses
Topics Covered

Overview

The nohup command in Linux is a handy tool that allows you to run processes even after logging out of a session. The term nohup stands for "no hangup", which prevents processes from receiving a hangup signal, stopping it from getting terminated after the user logs out of a session. This makes it useful when having long-running processes which you wish to keep running.

Introduction to Nohup Command in Linux

The nohup command is a built-in utility in Linux that allows you to run a process that continues to run even after you have logged out of a session. This command is particularly useful when you want to run a long-running process or a command that may take a significant amount of time to complete.

In essence, when you run a command using the nohup command, it prevents the process from receiving a hangup signal (SIGHUP) when you log out of your session. This signal is sent to all processes associated with your session when you log out. By default, the hangup signal causes the process to terminate, but with the use of nohup command, the process will continue to run in the background.

Linux Nohup Command Syntax

The basic syntax of the nohup command is as follows:

Here, options are optional command-line options that modify the behavior of the nohup command. The command is the name of the process or command that you want to run in the background. The arguments are any additional parameters that are required by the command. The output-file is an optional parameter that specifies the name of the file that the output will be redirected to. If you do not specify an output file, the output will be redirected to a file named "nohup.out" in the current directory.

Nohup Options

The nohup command provides a number of options that allow you to customize its behavior. Some of the most commonly used options include:

OptionDescription
-pThe -p option allows you to specify a process ID (PID) to send the HUP signal to. This can be useful if you want to terminate a background process that was started with nohup.
-nThis option prevents nohup from overwriting an existing output file. If an output file with the same name already exists, nohup appends the output to the end of the file.
-sThe -s option allows you to specify a signal to send to the process. By default, nohup sends the HUP signal, which tells the process to ignore hangup signals. However, you can use the -s option to send a different signal.
-fThis option tells nohup to force output to be written to the output file immediately instead of buffering it.
-hThis option tells nohup to send the hangup signal to the command. This is useful when you want to terminate a process that is running in the background.
-vThe -v option enables verbose output, which can be useful for debugging purposes.

How to Use Nohup Command in Linux

Here are the steps to use the nohup command in Linux:

Checking The Version of Nohup

Before using the nohup command, it's important to check the version of the command that is installed on the system. This can be done by running the following command:

This command will display the version of the nohup command installed on the system. If the command is not found, it may need to be installed using the package manager.

Starting A Process Using Nohup

To start a process using nohup, the following syntax can be used:

Here, the command is the name of the program or script that needs to be executed. The ampersand symbol at the end of the command is used to run the process in the background.

For example, to start a process that runs a Python script called "example.py" using nohup, the following command can be used:

This will start the Python script in the background, and any output generated by the script will be redirected to the "nohup.out" file.

Starting A Process in The Background Using Nohup

Sometimes, it may be necessary to start a process in the background using nohup. This can be done by running the following command:

Here, the "output-file" parameter is the name of the file where the output of the command will be redirected. The "2>&1" syntax is used to redirect both standard output and standard error to the output file.

For example, to start a process that runs a shell script called example.sh in the background using nohup, the following command can be used:

This will start the shell script in the background, and any output generated by the script will be redirected to the "output.log" file.

To Run Multiple Commands in The Background

To run multiple commands in the background using nohup, the following syntax can be used:

Here, the "command-1" to "command-n" parameters are the commands that need to be executed in the background.

For example, to start two processes that run a Python script called example.py and a shell script called example.sh in the background using nohup, the following command can be used:

This will start both the Python and shell scripts in the background, and any output generated by the scripts will be redirected to the "output.log" file.

Bringing Commands Back to The Foreground

To bring a command that is running in the background back to the foreground, the fg command is used. This fg command without any arguments will simply bring the most recent background process to the foreground.

In case, if multiple background processes are running, you can use the "jobs" command to list the processes and use the unique process number to bring a particular process to the foreground.

This will display a list of all the background processes that are currently running with a unique number assigned to each process.

To bring a particular process back to the foreground, you can use the following command:

For example, if you want to bring the process with the ID "1" back to the foreground, you can use the following command:

This will bring the process back to the foreground, and you can continue to interact with it as normal.

Conclusion

  • nohup stands for "no hang up", and is used to run commands/scripts without being affected by the hangup (HUP) signal.
  • The nohup command ensures that the command/script continues running even after the user logs out.
  • When using the nohup command, output, and error messages are redirected to a file named "nohup.out" by default.
  • The fg command can be used to bring a process running in the background to the foreground.

MCQs

  1. Which utility in Linux allows you to run a process that continues to run even after you have logged out of a session?
    • a. shutdown
    • b. nohup
    • c. cron
    • d. ps
  • Correct Answer: b. nohup
  1. What is the full form of the nohup command in Linux?
    • a. Non-Hungry User Program
    • b. No Hang Up
    • c. No Hurdles in Uploading Programs
    • d. None of the above
  • Correct Answer: b. No Hang Up
  1. Which signal does the nohup command prevent a process from receiving?
    • a. SIGSTOP
    • b. SIGTERM
    • c. SIGHUP
    • d. SIGKILL
  • Correct answer: c. SIGHUP