How to Use the Linux nohup Command to Keep Processes Running

When you start a process in a Linux terminal, that process is attached to the specific terminal session that launched it. If you close the terminal window, disconnect from an SSH session, or lose your network connection, the Linux kernel sends a SIGHUP (hangup) signal to the terminal. The terminal then passes this SIGHUP signal to all its child processes, causing them to terminate immediately.

This is problematic if you need to run a long-running script, a large file download, or a database backup that takes hours to complete. The nohup command (short for “no hangup”) solves this problem by intercepting the SIGHUP signal, allowing your process to keep running in the background even after you log out.

Basic Usage of nohup

To use nohup, simply prepend it to the command you want to run.

For example, to run a long Python data processing script:

nohup python3 process_data.py

When you press Enter, the command will execute, but you will notice two things:

  1. The terminal will print a message: nohup: ignoring input and appending output to 'nohup.out'
  2. The process will block your terminal (you won’t get your command prompt back until the script finishes).

Because you are likely planning to log out, you need to run the command in the background. To do this, append an ampersand (&) to the end of the command:

nohup python3 process_data.py &

Now, the script is running, immune to hangups, and you instantly get your command prompt back. You can safely type exit to close your SSH session, and the script will continue running on the server.

Understanding the nohup.out File

When you disconnect from the terminal, the background process can no longer print text to your screen. By default, nohup captures all standard output (stdout) and standard error (stderr) generated by your command and redirects it to a file named nohup.out in the directory where you ran the command.

You can check the progress of your long-running task by monitoring this file using the tail command:

tail -f nohup.out

This will print the last 10 lines of the file and continuously update the screen as new output is generated.

Redirecting Output to a Custom File

Relying on the default nohup.out file is not ideal if you are running multiple nohup commands simultaneously, as they will all try to write to the same file, creating a jumbled mess.

It is best practice to explicitly redirect the output to a named log file using standard Linux redirection operators:

nohup python3 process_data.py > data_job.log 2>&1 &

Here is how this command breaks down:

  • nohup: Protects the process from SIGHUP.
  • python3 process_data.py: The actual command being run.
  • > data_job.log: Redirects standard output (stdout) to data_job.log instead of nohup.out.
  • 2>&1: Redirects standard error (stderr) to the same place as standard output, ensuring error messages are captured in the log file.
  • &: Puts the job in the background.

How to Stop a nohup Process

Because the process is running in the background and is immune to hangups, you cannot stop it simply by pressing Ctrl+C or closing the terminal. You must manually terminate it using the kill command.

First, you need to find the Process ID (PID) of your running command. You can do this using the ps command combined with grep:

ps aux | grep process_data.py

Locate the PID in the second column of the output. Once you have the PID, terminate it gracefully:

kill 12345 (Replace 12345 with your actual PID).

If the process refuses to terminate (perhaps because it is hung on an I/O operation), you can force it to stop immediately using the SIGKILL signal:

kill -9 12345

By mastering the nohup command, you can confidently trigger massive data migrations, server updates, and automated scripts via SSH, knowing they will reliably complete their work even if your laptop disconnects from the network.

Leave a Reply

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

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.