Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Nohup Command in Linux?
3.
Nohup Command Syntax
4.
Nohup Command Options
5.
Working with nohup Command
5.1.
1. Checking the nohup Version
5.2.
2. Starting a Process using nohup Command
5.3.
3. Starting a Background Process using Nohup
5.4.
4. Run Multiple Commands in the Background using Nohup
6.
Limitation of Nohup Command
7.
Frequently Asked Questions
7.1.
What is the use of the nohup command in Linux?
7.2.
Which signal for a process is prevented by nohup command in Linux?
7.3.
How do I run a nohup command in the background?
7.4.
​​What is the difference between nohup and &?
8.
Conclusion
Last Updated: May 13, 2024
Medium

nohup Command in Linux with Examples

Author Jay Dhoot
0 upvote

Introduction

Commands in Linux start a process at its execution time, and the process automatically gets terminated upon exiting the terminal. This automatic termination of the running process might lead to a loss of data. 

Nohup Command in Linux

The Linux command "nohup" derives its name from "no hang up," permitting users to initiate commands or processes that persist even after they exit the terminal or terminate the session.

The nohup command in Linux can help to prevent the auto termination of the running process. It helps us to run a process even after the session is terminated. In this article, we will learn about the nohup command in Linux.

What is Nohup Command in Linux?

The nohup command is a native tool within Linux, designed to enable the execution of processes that persist even after you've exited your session. This is highly beneficial for managing tasks that are time-intensive or might require a substantial duration to finish.

It stands for ‘No Hang Up’ and is a built-in utility function that keeps the process running after the session is terminated. It prevents the process from receiving the SIGNUP signals (Signal Hang Up) that are sent to end or terminate a process. The nohup command is particularly useful when using SSH for remotely accessing systems. It ensures processes don't stop after the connection is interrupted.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Nohup Command Syntax

The nohup command in Linux can be used with the following syntax:
 

nohup [options] command [arguments ..] [output_file]

 

  1. options: This is an optional field. These command-line options change or modify the behavior of the nohup command.
     
  2. command: This is a mandatory field. The command specifies the name of the process or command that we want to run in the background.
     
  3. arguments: It refers to the additional parameters required for the command.
     
  4. output_file: This is an optional parameter that specifies the name of the file where the output will be redirected. By default, if no output_file is specified, the output is redirected to a file named “nohup.out” in the current directory.

Nohup Command Options

The options or flags of the nohup command allow you to customize its behavior and control how it manages the execution of your command in the background. Let's discuss some of the options below.

OptionsDescriptions
-pThis option specifies the process ID of a process you want to terminate.
-nThis option prevents an existing output file from being overwritten. Instead, it creates a new file if one already exists.
-sThis option specifies a signal to be sent to a process.
-fThis option forces the output to be written without waiting for it to buffer.
-hThis option sends a hangup signal to terminate a running process.
-vThis option enables verbose output for debugging.

Working with nohup Command

  • Checking the version of Nohup
  • Starting a Process Using Nohup
  • Starting a Process in the Background Using Nohup
  • Run multiple commands in the background

1. Checking the nohup Version

The nohup version can be checked using the following command:
 

nohup -- version


Output

Output


The above command displays the current version of the nohup command installed on the system. If the command is not found, we might need to install it using the package manager.

2. Starting a Process using nohup Command

The below steps would help us to create and start a process using the nohup command in Linux:
 

  • Let’s start by creating a new file (‘coding_ninjas.txt’) and append some text to that file.
     
cat > coding_ninjas.txt

     
    Add the text “Become a Ninja” and hit Ctrl + C which signals the terminal to save and exit.

Output

    The ^C in the output corresponds to the Ctrl + C.
 

  • Open the same file (‘coding_ninjas.txt’) with the nohup command.
     
nohup cat coding_ninjas.txt

     
    The cat command would execute the command even if the terminal gets terminated or closed.

Output

     The above output shows that the output is appended to nohop.out
 

  • To display the output, execute the following command:
     
cat nohup.out 
Output

 

  • To redirect the output to some other file say ‘xyz.txt,’ execute the following command:
     
nohup cat coding_ninjas.txt > xyz.txt
Output

     The command would copy the output to xyz.txt.
 

  • To check the output in the file ‘xyz.txt.’
     
cat xyz.txt
Output

3. Starting a Background Process using Nohup

To start a process in the background, use the ‘&’ symbol after the command.


Syntax

nohup command &

 

For example, 

nohup cat coding_ninjas.txt &


The above command will start the process coding_ninjas.txt in the background.

Output

Output

4. Run Multiple Commands in the Background using Nohup

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

nohup bash -c command1 & command2 & command3 & … & commandn &


Every command should be followed by ‘&’ to run in the background.
 

For example, 

nohup bash -c ‘echo “Coding Ninjas” & date’


Output

Output


The above command would run the print and date processes in the background. The output is redirected to nohup.out by default.

Limitation of Nohup Command

Some of the limitations of nohup command are:

  • When we start a process using nohup, it detaches from the terminal, meaning that the process doesn't have access to the standard user input. This limits the interaction of the process with the terminal.
     
  • As the process gets detached from the terminal, it becomes difficult to control it as there is no control terminal. This means some operations that depend on the terminal may not work appropriately.
     
  • The 'nohup' depends on the parent process, which means it inherits behaviors from the parent process. If the parent process terminates, the nohup process might also be terminated.
     
  • The 'nohup' command redirects the standard output and standard error to the nohup.out file by default. This helps capture the process output but may lead to large log files if it's not managed correctly.

Frequently Asked Questions

What is the use of the nohup command in Linux?

Nohup, which stands for 'no hang up,' is a Linux command used to ensure that processes continue to run uninterrupted even after the user has exited the terminal or shell.

Which signal for a process is prevented by nohup command in Linux?

Nohup is a tool that stops processes or jobs from getting the SIGHUP (Signal Hang UP) signal. This signal is usually sent to a process when you close or exit the terminal.

How do I run a nohup command in the background?

To run a command using "nohup" in the background, simply prepend "nohup" to the command, followed by the command itself and any arguments. For example: "nohup command &". This ensures the process continues running after you log out.

​​What is the difference between nohup and &?

The "nohup" command in Linux allows processes to continue running even after the user logs out, whereas appending "&" to a command runs it in the background but does not protect it from termination when the user logs out or the session ends.

Conclusion

In this article, we have discussed the nohup command in Linux. The "nohup" command in Linux is a valuable tool for ensuring uninterrupted execution of processes, even after logging out of a session. Its ability to detach processes from the terminal session enhances efficiency and productivity in various tasks, making it a valuable asset for Linux users.

To learn more about Linux commands, you can refer to the below-mentioned articles:

We hope this article has helped you understand the nohup command in Linux. If this article helped you in any way, then you can read more such articles on our platform, Code360. You will find articles on almost every topic on our platform. For interview preparations, you can read the Interview Experiences of popular companies.
 

Happy Coding !!

Previous article
xxd Command in Linux
Next article
Difference between Linux and Windows
Live masterclass