When you log into a remote Linux server via SSH to perform maintenance, any commands you execute are inherently tied to your active terminal session. This is a fundamental security architecture of Linux. However, it presents a massive problem: if you start a command that takes six hours to complete (such as compiling a large software package from source, or transferring 100GB of database backups), you cannot close your laptop, put it to sleep, or disconnect from the Wi-Fi. If your SSH connection drops for even a microsecond, the Linux server will instantly send a “SIGHUP” (Hangup Signal) to your terminal, immediately killing your long-running script. To prevent this disaster, you must launch your scripts using the nohup (No Hangup) command.
How the nohup Command Works
The nohup command acts as a protective shield around your process. It intercepts the deadly SIGHUP signal sent by the operating system when you disconnect, silently discarding it so your script can continue running orphaned in the background.
To use it, you simply prepend the word nohup to your normal command. Furthermore, because you plan on logging out, you must also append an ampersand (&) at the end of the line to detach the process from your active screen and push it into the background.
- Open your terminal and connect to your server.
- Type the command:
nohup ./compile_massive_software.sh & - Press Enter.
The terminal will instantly output a message that looks like this:
[1] 23456
nohup: ignoring input and appending output to 'nohup.out'
You can now safely type exit to close your SSH session, slam your laptop shut, and go home. The server will continue executing your script flawlessly.
Understanding the nohup.out Log File
Because you are no longer watching the screen, where do the error messages and print statements from your script go? By default, the nohup command automatically creates a plain text file named nohup.out in the exact directory where you launched the command.
Every single line of text that your script normally would have printed to your terminal screen is instead redirected and saved into this file.
When you log back into the server the next morning to check on your script, you can read the progress log using the tail command:
tail -f nohup.out
This command streams the bottom of the log file live to your screen, allowing you to watch the background process work in real-time.
Redirecting Output to a Custom File
If you launch three different nohup commands in the same directory, they will all dump their output into the exact same nohup.out file, creating an unreadable mess of intertwined logs. To solve this, you can use standard bash redirection brackets (>) to force the output into a custom file.
nohup ./database_backup.sh > db_backup.log 2>&1 &
> db_backup.logforces the standard output into a specifically named file.2>&1forces the standard error messages to be written into the exact same log file, ensuring you don’t miss any critical failure alerts.