If you connect to a remote Linux server via SSH and initiate a massive, three-hour database compilation script, you cannot simply close your laptop or shut down your terminal window. The moment your SSH connection drops, the Linux server will instantly terminate all running processes tied to your specific session, killing your script instantly. To force the server to keep working even after you disconnect, you must use the nohup command.
What Does Nohup Do?
The term nohup stands for “No Hangup.” In ancient computing days, dropping a telephone modem connection would send a “SIGHUP” (Signal Hangup) to the server, telling it to kill the user’s jobs. The nohup command intercepts this signal and mathematically blocks it, ensuring the process survives indefinitely in the background.
How to Use the Command
Using the tool is incredibly simple. You do not need to rewrite your scripts; you simply place the word nohup at the very beginning of your standard command, and an ampersand (&) at the very end.
- Open your Linux terminal and SSH into your server.
- Suppose your normal, long-running command is:
python3 data_parser.py - To run it safely, modify it to look exactly like this:
nohup python3 data_parser.py &
- Press Enter.
Managing the Background Output
When you press Enter, the terminal will instantly output a process ID number (e.g., [1] 45982) and return you to a clean prompt. The script is now safely running in the background.
You can now safely type exit, close your terminal, shut down your laptop, and go to sleep. The server will not kill the script.
Where does the output go?
Because you are no longer watching the screen, Linux has to store the script’s output somewhere. By default, nohup will automatically create a brand new text file named nohup.out in the exact directory where you ran the command. All print statements, warnings, and error messages generated by your script will be safely appended to this text file, allowing you to easily review the logs the next morning.