The nohup
command in Linux is used to run a command or script in the background, detached from the current shell session. It allows the command or script to continue running even after the user logs out or the terminal session is terminated. The name “nohup” stands for “no hangup.”
Here’s an overview of how to use the nohup
command:
- Basic Usage:
To run a command or script usingnohup
, simply prependnohup
before the command or script name. For example:
nohup command-to-run &
This command runs command-to-run
in the background, detached from the current shell session.
- Output and Error Redirect:
By default,nohup
redirects the command’s output and error streams to a file namednohup.out
in the current directory. You can specify a different output file using the>
or>>
redirect operators. For example:
nohup command-to-run > output.txt &
This command redirects the output of command-to-run
to the output.txt
file.
- Disowning the Process:
When you run a command withnohup
, it is still associated with the current shell session. If you want to disown the process and detach it completely from the shell, you can use thedisown
command. For example:
nohup command-to-run &
disown
This sequence of commands runs command-to-run
in the background with nohup
and then disowns the process.
- Examples:
- Running a script in the background:
nohup ./script.sh &
This command runs thescript.sh
script in the background, detached from the current shell session. - Redirecting output to a file:
nohup command-to-run > output.txt &
This command runscommand-to-run
in the background and redirects its output to theoutput.txt
file. - Disowning a process:
nohup command-to-run & disown
This sequence of commands runscommand-to-run
in the background withnohup
and then disowns the process.
The nohup
command is particularly useful when you want to run long-lasting or resource-intensive commands or scripts that should continue running even if you log out or close the terminal session. It allows you to run processes in the background without being tied to the current shell.
Note that while nohup
prevents the command from being terminated by a hangup signal, it does not prevent it from being terminated by other means, such as a system shutdown or a manual kill command.
For more information about the nohup
command and its options, you can refer to the manual page by typing man nohup
in your terminal.