How to Keep a Linux Terminal Process Running After You Disconnect Using the nohup Command

If you launch a massive script to compile source code, download a gigantic database, or compress a massive video file on a remote Linux server, the process might take several hours to complete. If your local internet connection drops, or if you simply close your SSH terminal window on your laptop to go home for the day, the server will instantly kill the process you started, wasting hours of progress.

By default, when a terminal session ends (a “hangup” signal), all child processes attached to that terminal are terminated. To protect a critical command from this execution, you must use the nohup (no hangup) command.

How to Use the nohup Command

The nohup command acts as a protective shield around your primary command, instructing the Linux kernel to explicitly ignore the termination signal when you close the window.

  1. Open your terminal and SSH into your server.
  2. Instead of typing your command normally (e.g., python3 long_script.py), simply place the word nohup in front of it, and add an ampersand (&) at the very end to push the task into the background.
    nohup python3 long_script.py &
  3. Press Enter.

The terminal will instantly return a process ID number (e.g., [1] 48932) and print a message saying: “nohup: ignoring input and appending output to ‘nohup.out'”.

You can now type exit to safely close the terminal, shut your laptop, and go home. The Python script will continue running on the server flawlessly.

How to Check the Output Later

Because you pushed the command to the background and closed the window, you cannot see the text it is printing to the screen. To solve this, nohup automatically captures every single line of text your script outputs and saves it into a text file named nohup.out in the exact directory where you launched the command.

When you reconnect to the server the next morning, simply use the cat or tail command to read that log file and check on the status of your script:

tail -f nohup.out

This will print the last 10 lines of the output to your screen, proving that the script successfully finished its job overnight.

Get the best tech tips delivered straight to your inbox.

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