Linux nohup command

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:

  1. Basic Usage:
    To run a command or script using nohup, simply prepend nohup 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.

  1. Output and Error Redirect:
    By default, nohup redirects the command’s output and error streams to a file named nohup.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.

  1. Disowning the Process:
    When you run a command with nohup, 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 the disown 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.

  1. Examples:
  • Running a script in the background: nohup ./script.sh & This command runs the script.sh script in the background, detached from the current shell session.
  • Redirecting output to a file: nohup command-to-run > output.txt & This command runs command-to-run in the background and redirects its output to the output.txt file.
  • Disowning a process:
    nohup command-to-run & disown
    This sequence of commands runs command-to-run in the background with nohup 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.