The Wayback Machine - https://web.archive.org/web/20240712202246/https://www.geeksforgeeks.org/nohup-command-in-linux-with-examples/
Open In App

nohup Command in Linux with Examples

Last Updated : 29 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Every command in Linux starts a process at the time of its execution, which automatically gets terminated upon exiting the terminal. Suppose, you are executing programs over SSH and if the connection drops, the session will be terminated, all the executed processes will stop, and you may face a huge accidental crisis. In such cases, running commands in the background can be very helpful to the user and this is where nohup command comes into the picture. nohup (No Hang Up) is a command in Linux systems that runs the process even after logging out from the shell/terminal. 

Nohup Command

Usually, every process in Linux systems is sent a SIGHUP (Signal Hang UP) which is responsible for terminating the process after closing/exiting the terminal. Nohup command prevents the process from receiving this signal upon closing or exiting the terminal/shell. Once a job is started or executed using the nohup command, stdin will not be available to the user and nohup.out file is used as the default file for stdout and stderr. If the output of the nohup command is redirected to some other file, nohup.out file is not generated. 

Nohup Command Syntax

The syntax for using the Nohup command is straightforward:

nohup command [options] &
  • `command`: Specifies the command or script that you want to execute.
  • `[options]`: Optional arguments or flags that modify the behavior of the command.
  • `&`: Placing an ampersand (&) at the end of the command instructs the shell to run the command in the background.

Checking the version of Nohup

Checking the version of Nohup is a simple process. You can typically check the version of Nohup installed on your system by using the `--version` flag. Simply execute the following command:

nohup --version

Output:

nohup version

Starting a Process Using Nohup

To start a process using Nohup, simply prepend the desired command with nohup. For example, if you want to execute a bash script named geekfile.py using Nohup, you would use the following command:

nohup bash geekfile.sh

Output:

nohub bash

To redirect the output to the output.txt file:

nohup bash geekfile.sh > output.txt

Output:

nohu bash output.txt

Starting a Process in the Background Using Nohup

To run the command in the background, the ‘&’ symbol is appended at the end of the command. After executing, it doesn’t return to the shell command prompt after running the command in the background. It can be brought back to the foreground with the fg command.

nohup bash geekfile.sh &
fg

Output:

nohup bash script

Note: The number within square brackets represents the job id and the number next to it is the process id.

To run multiple commands in the background

nohup command can be used to run multiple commands in the background.

nohup bash -c 'commands'

Example:

nohup bash -c 'cal && ls'

Output:

To run multiple commands in the background:

Here, the output will be by default stored in nohup.out. To redirect it, type:

nohup bash -c 'commands' > filename.txt

Example:

nohup bash -c 'cal && ls' > output.txt

Output:

Storing the output in output file

nohup Command in Linux – FAQs

What is the nohup command in Linux, and what does it do?

The nohup command stands for “no hang up.” It allows you to run a command or shell script in such a way that it continues to run even after you log out or close the terminal. This prevents the command from being terminated when the terminal session ends.

How do I use the nohup command?

To use nohup, simply prepend it to the command you want to run. For example:

nohup command_to_run &

The & symbol at the end puts the command in the background, allowing you to continue using the terminal while the command runs.

Where does nohup send the output of the command?

By default, nohup redirects both standard output (stdout) and standard error (stderr) to a file named nohup.out in the current directory. You can specify a different output file if desired.

Can I use nohup with interactive commands or programs?

While nohup is typically used with non-interactive commands or scripts, you can use it with interactive commands. However, the behavior may be unpredictable, especially if the command expects input from the user. It’s generally recommended to avoid using nohup with interactive programs.

How can I check the status of a command running with nohup?

Since nohup runs the command in the background, you can use standard Linux tools like ps or top to check the status of the process. For example:

ps aux | grep <command_name>

This will show you information about the process, including its PID (Process ID), which you can then use to manage or monitor the command.

Conclusion

In Linux, when you run a command, it usually stops when you close the terminal. But with nohup, you can keep it running even after you’ve closed the terminal. This is super handy, especially if you’re running commands remotely and don’t want them to stop if your connection drops. nohup works by shielding your command from signals that would normally stop it. You just add nohup before your command, and it keeps going. You can also send the command’s output to a file. Just remember, it’s best for non-interactive tasks, and you can check on your command’s status using tools like ps. So, nohup is like a safety net for your commands, ensuring they keep chugging along even when you’re not watching.



Previous Article
Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the vario
4 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
ZIP command in Linux with examples
ZIP is a compression and file packaging utility for Unix. Each file is stored in a single .zip {.zip-filename} file with the extension .zip. Zip is used to compress files to reduce file size and is also used as a file package utility. Zip is available in many operating systems like Unix, Linux, windows, etc.If you have limited bandwidth between two
7 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
comm command in Linux with examples
comm compare two sorted files line by line and write to standard output; the lines that are common and the lines that are unique. Suppose you have two lists of people and you are asked to find out the names available in one and not in the other, or even those common to both. comm is the command that will help you to achieve this. It requires two so
5 min read
cmp Command in Linux with examples
cmp command in Linux/UNIX is used to compare the two files byte by byte and helps you to find out whether the two files are identical or not. When cmp is used for comparison between two files, it reports the location of the first mismatch to the screen if difference is found and if no difference is found i.e the files compared are identical.cmp dis
5 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
df Command in Linux with examples
There might come a situation while using Linux when you want to know the amount of space consumed by a particular file system on your LINUX system or how much space is available on a particular file system. LINUX being command friendly provides a command line utility for this i.e df command that displays the amount of disk space available on the fi
8 min read
tee command in Linux with examples
tee command reads the standard input and writes it to both the standard output and one or more files. The command is named after the T-splitter used in plumbing. It basically breaks the output of a program so that it can be both displayed and saved in a file. It does both the tasks simultaneously, copies the result into the specified files or varia
2 min read