nohup

process managementLinux/Unix
The nohup command is one of the most frequently used commands in Linux/Unix-like operating systems. nohup Run a command immune to hangups, with output to a non-tty

Quick Reference

Command Name:

nohup

Category:

process management

Platform:

Linux/Unix

Basic Usage:

nohup [options] [arguments]

Common Use Cases

    Syntax

    nohup COMMAND [ARG]...

    Options

    Option Description
    --help Display help information and exit
    --version Output version information and exit

    Common Output Redirection with nohup:

    Syntax Description
    nohup command > output.log Redirect stdout to output.log (stderr still goes to nohup.out)
    nohup command > output.log 2>&1 Redirect both stdout and stderr to output.log
    nohup command > /dev/null 2>&1 Discard all output (both stdout and stderr)
    nohup command > output.log 2> error.log Redirect stdout to output.log and stderr to error.log

    Examples

    How to Use These Examples

    The examples below show common ways to use the nohup command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    nohup ls -la
    Run ls -la command, ignoring hangup signals.
    nohup long_running_command &
    Run a long-running command in the background, immune to hangup signals.
    # Advanced Examples Advanced
    nohup ./my_script.sh > my_script.log 2>&1 & Run my_script.sh in the background, redirecting both stdout and stderr to my_script.log. nohup python3 data_processor.py > output.log < input.txt & Run a Python script in the background with input from input.txt and output to output.log. nohup bash -c "for i in {1..10000}; do echo $i; sleep 1; done" > counting.log 2>&1 & Run a bash loop in the background that counts to 10000, with output to counting.log. nohup find / -name "*.txt" > find_results.txt 2> find_errors.txt & Run find command in the background with stdout and stderr redirected to separate files. nohup ./backup.sh & echo $! > backup.pid Run backup.sh in the background and save its process ID to backup.pid file. nohup ./server_process > /dev/null 2>&1 & Run server_process in the background, discarding all output. nohup make -j4 > compile.log 2>&1 & Compile a program with 4 parallel jobs in the background, saving output to compile.log. (cd /path/to/project && nohup ./start_server.sh > server.log 2>&1 &) Change to a directory, then run start_server.sh in the background with output to server.log.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The nohup command, short for "no hangup", is a POSIX command that is essential for ensuring processes continue running even after the user who started them has logged out or disconnected from the system. It's particularly valuable in remote server environments, where maintaining long-running processes is critical. Key features of the nohup command: 1. Signal Immunity: The primary function of nohup is to make a command immune to the SIGHUP (hangup) signal. This signal is typically sent to processes when the terminal controlling them is closed, such as when a user logs out or a connection drops. By ignoring this signal, the process can continue running uninterrupted. 2. Output Redirection: By default, if standard output is a terminal, nohup redirects the command's output to a file named "nohup.out" in the current directory. If that file isn't writable, it uses "$HOME/nohup.out". This ensures that output from the process isn't lost when the terminal closes. 3. Background Processing: While nohup itself doesn't put processes in the background, it's commonly used with the shell's background operator (&) to create processes that both run in the background and survive terminal disconnections. 4. Persistence Across Sessions: The combination of hangup immunity and output redirection makes nohup perfect for running commands that need to persist across login sessions, such as server processes, batch jobs, or long-running data processing tasks. Common use cases for nohup include: - Running server applications that need to continue operating after the administrator logs out - Starting batch processing jobs that may take longer than a login session - Initiating system maintenance tasks that should continue despite connection issues - Running compilation or build processes that are time-consuming - Starting data backup or synchronization processes that need to complete regardless of user session status While nohup is powerful in its simplicity, modern systems often provide alternative methods for detaching processes from terminals, such as the 'screen' or 'tmux' utilities, which offer more features like the ability to reattach to the running process later. Systemd services on Linux systems also provide a more structured way to manage long-running processes. It's important to note that nohup only handles the SIGHUP signal; it doesn't protect against other signals that might terminate a process, such as SIGTERM or SIGKILL. Also, nohup doesn't change the scheduling priority of the process, unlike commands like 'nice'. When using nohup, it's a good practice to explicitly redirect output to specific log files rather than relying on the default nohup.out, especially in environments where multiple nohup processes might be running. This makes it easier to track and debug the output of different processes. The nohup command is available on virtually all Unix and Linux systems, making it a reliable tool for ensuring process continuity across different environments.

    Related Commands

    These commands are frequently used alongside nohup or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the nohup command works in different scenarios.

    $ nohup
    View All Commands