The nohup command in Linux: running processes immune to termination

What is nohup?

The command nohup (short for “no hang up”) is an essential tool in Unix-like systems that allows executing a process so that it ignores the SIGHUP signal, which is sent to processes when the terminal session is closed. Without nohup, when closing the session or disconnecting via SSH, the child process would receive SIGHUP and terminate abruptly, which can cause data loss or interruption of critical tasks. With nohup, the process continues its execution in the background, and its standard output and error are redirected, by default, to a file named nohup.out in the current directory, unless another destination is specified. This feature makes nohup ideal for backup scripts, long compilations, system updates, or any task that needs to remain active after the user leaves the terminal.

Basic syntax

The simplest way to use nohup is:

nohup command [arguments] &

The ampersand (&) at the end sends the process to the background after nohup has launched it. If the & is not included, the process will run in the foreground and the terminal will be blocked until it finishes, although it will still ignore SIGHUP. Additionally, it is possible to redirect standard output and error explicitly:

nohup command > output.log 2> error.log &

This way, the administrator can control where the logs are stored and prevent the nohup.out file from growing uncontrollably. It is also possible to combine nohup with nice or ionice to adjust the CPU or I/O priority of the process.

Usage examples

  • Running a nightly backup script:
    nohup /usr/local/scripts/backup.sh > /var/log/backup_nohup.out 2>&1 &
  • Compiling a large project:
    nohup make -j$(nproc) > build.log 2>&1 &
  • Running a development server in the background:
    nohup python3 manage.py runserver 0.0.0.0:8000 > server.log 2>&1 &
  • Remote package update via SSH:
    nohup apt-get update && apt-get upgrade -y > /tmp/upgrade.log 2>&1 &

In each case, the process continues even if the SSH session closes, and the logs are saved in the specified files for later review.

Differences with & and disown

The & operator only places a job in the background, but does not protect it from the SIGHUP signal; if the terminal closes, the job will receive the signal and terminate, unless it has been previously detached. The disown command, available in shells like Bash and Zsh, removes the job from the shell’s job table, preventing the shell from tracking it and thus from sending SIGHUP when it closes. However, disown must be executed after the process has been started, and it does not automatically redirect output. In contrast, nohup acts preventively: before launching the process, it tells it to ignore SIGHUP and, by default, redirects its output to nohup.out. Therefore, nohup is the most direct option when you need to guarantee the continuity of a process from the moment of its launch.

  • & → background, vulnerable to SIGHUP.
  • disown → removes from job control, but requires subsequent action.
  • nohup → ignores SIGHUP from the start and manages output.

Best practices

  • Periodically review the nohup.out file or custom logs to detect errors or unexpected messages.
  • Use log rotation (e.g., with logrotate) if the process generates a lot of output, to avoid filling the disk.
  • Combine nohup with multiplexing tools like screen or tmux when occasional interaction with the process is needed.
  • Avoid running commands that require interactive input under nohup

    This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .