nohup - Linux


Overview

nohup, short for “no hangup”, is a POSIX command used to run another command with hangup signals ignored, enabling the command to continue running in the background even after the user has logged out. This command is particularly useful for long-running processes in remote servers and batch jobs that need to continue running regardless of the user’s session status.

Syntax

The basic syntax of nohup is as follows:

nohup <command> [arguments] &
  • <command>: This is the command you want to run without hangup.
  • [arguments]: These are optional arguments for the command.
  • &: When appended at the end, this symbol sends the command to run in the background.

Options/Flags

nohup has a very limited set of options:

  • --help: Display a help message and exit.
  • --version: Output version information and exit.

Because nohup is a straightforward utility, most of its functionality revolves around the command it is paired with rather than options within nohup itself.

Examples

  1. Basic Example

    Running a Python script in the background:

    nohup python myscript.py &
    
  2. Output Redirection

    Redirecting output to a file other than nohup.out:

    nohup ./my_script.sh > my_output.log 2>&1 &
    

    Here, > my_output.log redirects standard output to my_output.log, 2>&1 redirects standard error to standard output, ensuring all output goes to the same file.

  3. Combining with tail to Monitor Output

    nohup ./long_running_process.sh > process.log 2>&1 &
    tail -f process.log
    

    This approach allows you to start a process in the background but immediately start watching its output using tail.

Common Issues

  • Output Issues: If nohup is invoked without redirecting the output, it writes the output to a file named nohup.out in the current directory. If it cannot write to this file, it attempts to write to nohup.out in the user’s home directory. If it cannot write there, the command fails.

  • Ignoring Input: nohup ignores all input (stdin is redirected from /dev/null). Any command that requires user input after invocation will not work correctly when called with nohup.

Integration

nohup is often combined with other terminal commands to achieve uninterrupted server processes:

  • Script Execution at Boot-time

    You can include nohup in a startup script to ensure that specific commands persist after the boot process:

    nohup /path/to/script.sh &
    
  • Cron Jobs

    Although cron jobs do not typically require nohup, in complex scripts where subprocesses might hang up due to user logouts, wrapping the entire cron job or specific commands in nohup can prevent unintended exits.

  • &: Used to run any command in the background in Linux.
  • disown: Removes jobs from the shell’s job table, making them immune to hangup signals.
  • screen or tmux: More advanced tools that allow for detaching and reattaching to shell sessions.

For further reading and additional details, you might visit the official GNU coreutils page or consult the man pages by typing man nohup in your terminal.