Nohup

Why Trust Techopedia

What is Nohup?

Nohup (no hang up) is an operating system (OS) utility that allows Linux and other Unix-like systems to run a command or script in the background even after the user has logged out or the terminal has been closed. Nohup is especially useful for running long processes that don’t require user interaction to continue running.

Advertisements

The nohup meaning stands for “no hangup.” Essentially, the nohup command prevents the command before it from receiving a SIGHUP (Signal Hang UP) signal. SIGHUP is a signal that gets sent to all processes that were initiated from that terminal when the controlling terminal is closed.

Techopedia Explains the Nohup Meaning

Techopedia Explains the Nohup Meaning

The term “hang up” originated from the days when connections to remote computers were made over telephone lines. When you finished using a terminal, you would “hang up” the phone and discontinue your session. This would terminate every process that was started during the session.

How Nohup Works

The nohup definition refers to a command that prevents processes from terminating when their controlling terminal closes. When you prefix a command with nohup and run it from a terminal, nohup will ignore the hangup signal that the operating system sends when the terminal closes.

For example, if you wanted to run a script named script.sh and ensure it continues to run even if you log out, you would use:

script.sh

The & at the end puts the command in the background, and nohup ensures it keeps running nohup ensures it keeps running if the terminal closes.

After it closes, output that would normally go to the terminal will be redirected to a file named nohup.out in the current directory – unless the output is redirected someplace else.

If the user does not have write permission in the current directory or a nohup.out file cannot be created for some reason, the nohup command will attempt to write the output to ~/nohup.out in the user’s home directory. The redirection ensures that output won’t be lost after the user logs out.

The user can also specify their own redirection. In this case, nohup will not create the nohup.out file. The output will go to the specified file instead.

Note: In most Linux distributions, when you use the nohup command to start a process, it changes the signal disposition for SIGHUP (hangup signal) to SIG_IGN (ignore).  This instructs the operating system to ignore the hangup signal for that specific process.

Nohup Command Syntax

The nohup command syntax is quite straightforward. The basic syntax of the nohup command looks like this:

Nohup Command Syntax

Here’s a breakdown of the nohup command syntax:

Nohup
This tells the shell to run the following command with the no-hangup functionality.

Command
This is the actual command you want to run without hangup. It could be any command, script, or executable file you’d typically run in the shell.

arguments
These are optional arguments or parameters you want to pass to the command. Not all commands require arguments, but if your command does, you can list them here.
&
Technically, this symbol is not part of the nohup command but is often used with nohup. (It instructs the shell to run the command in the background.) Without the &, the command would still avoid hangup, but it would run in the terminal until it finished. & allows you to get your terminal prompt back so you can continue working or log out.

Nohup Examples

Here are three examples of how to use nohup in three different contexts:

  1. Running a Script in the Background

    If you have a script named long_running_script.sh that performs tasks like data backups or processing batch jobs, you can run it with nohup to ensure the script will continue running even if you log out.

    Running a Script in the Background

    The command above will run long_running_script.sh, ignore the hangup signal, and continue running the process in the background. Output will be redirected to nohup.out by default.

  2. Custom Output Redirection

    By default, nohup redirects the standard output (stdout) and standard error (stderr) of the command to a file named nohup.out in the working directory. If nohup is unable to write to this file, it will attempt to write to ~/nohup.out in the user’s home directory. Users can also manually redirect the output by specifying a different file, using shell redirection:

    Custom Output Redirection

    In this example:

    • redirects standard output (stdout) to my_output.log.
    • 2>&1 redirects standard error (stderr) to the same place as stdout.
    • & at the end puts the command in the background.
  3. Running a Command That Accepts Input From a File

    If you have a command that needs to process input from a file, and you want to ensure it keeps running in the background, you can use nohup as follows:

    Running a Command That Accepts Input From a File

    This example takes an unsorted list from unsorted_list.txt, sorts it, and outputs the sorted list to sorted_list.txt.

    Even though “sort” might not be a long-running command, this example demonstrates how nohup can be used for commands that read from and write to files. The & puts the sort command in the background and allows you to continue using the terminal or log out.

    Note: In all the examples above, it’s important to remember that:

    • The & at the end is important for immediately running the process in the background. Without it, nohup will prevent a process from stopping when the user logs out, but the terminal won’t be able to be used until the process finishes.
    • The output will go to nohup.out by default unless the user redirects it.
    • To ensure that there are no running issues with scripts or commands (permission problems or path issues, for example), it’s a good idea to test them in an active session before using nohup.

    Nohup Pros and Cons

    Using nohup to run commands or scripts on Unix-like operating systems has several advantages, but there are also limitations to keep in mind. Nohup is a valuable tool, but it’s important to use it for scenarios where its advantages outweigh the limitations.

    Pros

    • Background execution
    • Persistence after logout
    • Simple and easy to use
    • Automatic output redirection

    Cons

    • No automatic recovery
    • Limited control over running processes
    • File management
    • Potential for overlooked errors
    • Security considerations

    The Bottom Line

    Nohup is a valuable command for running long-term, low-maintenance processes in the background. It allows them to run to completion without needing an open terminal session and ensures that critical tasks are not interrupted by session endings or accidental terminal closures.

    FAQs

    What is nohup in simple terms?

    What is the use of nohup Linux?

    How do I run a nohup process in the background?

    What is the nohup source command?

    Advertisements

    Related Questions

    Related Terms

    Margaret Rouse
    Editor

    Margaret jest nagradzaną technical writerką, nauczycielką i wykładowczynią. Jest znana z tego, że potrafi w prostych słowach pzybliżyć złożone pojęcia techniczne słuchaczom ze świata biznesu. Od dwudziestu lat jej definicje pojęć z dziedziny IT są publikowane przez Que w encyklopedii terminów technologicznych, a także cytowane w artykułach ukazujących się w New York Times, w magazynie Time, USA Today, ZDNet, a także w magazynach PC i Discovery. Margaret dołączyła do zespołu Techopedii w roku 2011. Margaret lubi pomagać znaleźć wspólny język specjalistom ze świata biznesu i IT. W swojej pracy, jak sama mówi, buduje mosty między tymi dwiema domenami, w ten…