nohup Command in Linux



The nohup (short for "no hang up") command in Unix and Linux allows a command to continue running even after the user has logged out. It's particularly useful for long-running processes that you don't want to interrupt when you disconnect from the session.

When a command is run with nohup and followed by &, it places the command in the background, allowing you to continue using the terminal. In interactive shells, the nohup command can be handy when running scripts remotely via SSH, ensuring the script continues even if the SSH session is terminated.

Input and Output Handling with nohup

  • Standard Input (stdin) − When you run a command in a terminal, the standard input is the keyboard input you provide. If the command expects input from the terminal and you're not around to provide it (because you logged out), nohup will redirect that input to an unreadable file. This is to prevent the command from hanging, waiting for input that will never come.
  • Standard Output (stdout) − This is the normal output stream where your command sends its output (usually displayed on the terminal screen). If your terminal is closed, nohup will try to save this output to a file called nohup.out in the current directory. If it can't create this file in the current directory, it will use $HOME/nohup.out instead. This ensures that all the command's output is saved somewhere you can later review.
  • Standard Error (stderr) − This is the stream where your command sends error messages.nohup will redirect any error messages to the same place as the standard output, which means both regular output and error messages will go to the same file (nohup.out or $HOME/nohup.out).

This way, nohup ensures that the command keeps running smoothly, and all output (both regular and errors) is captured in one place you can check later.

Exit Status

  • 125 − If the nohup command itself fails.
  • 126 − If COMMAND is found but cannot be invoked.
  • 127 − If COMMAND cannot be found.
  • Other − The exit status of COMMAND otherwise.

Table of Contents

Here is a comprehensive guide to the options available with the nohup command −

Syntax of nohup Command

The basic syntax for the nohup command is as follows −

nohup COMMAND [ARG]...

Example of nohup Command in Linux

In this section, we'll walk through a scenario where you need to run a long-running backup script on your server, ensuring it continues to run even if your session gets disconnected.

Running a Backup Script

Suppose you have a script named backup.sh that performs a full backup of your server. This script takes several hours to complete, and you don't want to risk it being interrupted if you lose your SSH connection to the server. Using the nohup command can help you avoid this issue.

To get started, create a new file named backup.sh and add the following contents to the file −

#!/bin/bash
# Source directory
SOURCE="/home/user/documents"

# Destination directory
DESTINATION="/home/user/backup"

# Perform backup using rsync
rsync -av --delete "$SOURCE" "$DESTINATION"
echo "Backup completed successfully."

Save the file and make it executable −

sudo chmod +x backup.sh

Now, you can run the script using the nohup command to ensure it keeps running even if you disconnect from the terminal −

sudo nohup ./backup.sh > backup_output.log 2>&1 &

Explanation

  • nohup − Ensures the command keeps running even after you log out.
  • ./backup.sh − The script you're running.
  • backup_output.log − Redirects standard output to backup_output.log.
  • 2>&1 − Redirects standard error to the same file as standard output.
  • & − Puts the command in the background.

By running the command with nohup and redirecting the output to a log file, you've ensured that your backup process will continue running even if your terminal session ends.

nohup Command in Linux1

[1] − The job number of the background process.

62655 − The process ID (PID) of the running script.

You can keep an eye on the running process by using the ps command −

sudo ps aux | grep backup.sh
nohup Command in Linux2

You can also monitor the output of your script in real-time by using the tail command −

sudo tail -f backup_output.log
nohup Command in Linux3

The message nohup: ignoring input indicates that nohup is working as expected by ignoring input from the terminal, ensuring that the script runs uninterrupted.

Conclusion

The nohup command is a valuable tool for ensuring uninterrupted execution of long-running processes in Unix and Linux environments. By preventing commands from being terminated when a user logs out or disconnects, nohup provides stability and reliability for critical operations like backups and data processing.

Its capability to capture both standard output and errors in a log file simplifies monitoring and troubleshooting. Whether managing remote scripts or automating server tasks, understanding and utilizing nohup empowers system administrators to maintain efficient and resilient workflows.