How to Keep a Linux Process Running in the Background After Closing the Terminal Using nohup

When you launch a long-running process in the Linux terminal—such as compiling a massive software package, downloading a 50GB dataset, or running a complex Python web scraper—that process is inherently tied to your active terminal session.

If your SSH connection drops due to a network timeout, or if you accidentally close the terminal window on your desktop, the shell will immediately send a SIGHUP (Signal Hang Up) command to the active process, instantly terminating it and destroying hours of work.

To prevent this, you must instruct Linux to detach the process from the terminal using the nohup (No Hang Up) command.

How to Use the nohup Command

Using nohup is incredibly simple; you just place the word nohup directly in front of whatever command you were originally going to run.

For example, if your original command was a Python script:

python3 web_scraper.py

To run it securely in the background, you would execute:

nohup python3 web_scraper.py &

Notice the & (ampersand) at the very end of the command. While nohup prevents the hang-up signal, the ampersand tells the terminal to push the process into the background immediately, giving you your command prompt back so you can continue typing other commands.

Where Does the Output Go?

Because the process is running in the background, it cannot print errors or progress updates to your screen. Instead, nohup automatically creates a text file named nohup.out in the directory where you executed the command.

Every single line of terminal output generated by your script will be safely appended to this text file.

You can check the live progress of your background script at any time by monitoring the end of that text file using the tail command:

tail -f nohup.out

How to Stop a nohup Process

Because the process is detached, you cannot stop it using the standard Ctrl+C shortcut.

  1. First, find the Process ID (PID) by searching for the command name:
    ps aux | grep web_scraper.py
  2. The terminal will return a line of data. The number in the second column is your PID (e.g., 4052).
  3. Terminate the script manually using the kill command:
    kill 4052

Get the best tech tips delivered straight to your inbox.

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