How to Keep Commands Running After Logout Using nohup in Linux

When you initiate a massive, multi-hour bash script or a complex database compilation via SSH on a remote Linux server, your execution is inextricably tied to your active terminal session. If your local Wi-Fi drops or you physically close your laptop, the SSH connection severs. The Linux kernel will instantly detect this hangup, transmit a lethal SIGHUP (Signal Hangup) to your active processes, and violently terminate your script. To mathematically immunize your execution from this termination signal, you must wrap your command in the nohup architecture.

Understanding the Immunity Matrix

The nohup (No Hangup) command is a deeply powerful execution wrapper. It acts as an impenetrable shield between your process and the Linux kernel’s session management daemon. When the kernel attempts to transmit the SIGHUP kill command, the nohup wrapper intercepts the signal and mathematically drops it into a void, allowing your script to continue executing perpetually in the background.

Executing the Shielded Process

Imagine you need to execute a massive backup script named database_dump.sh that will mathematically take 14 hours to complete, and you need to power down your local workstation immediately.

To execute the shielded script, open your terminal and type:

nohup ./database_dump.sh &

The syntax requires a deep breakdown:

  • nohup: Initializes the signal immunity wrapper.
  • ./database_dump.sh: The target command or script you are executing.
  • &: This critical ampersand at the absolute end of the command instructs the kernel to instantly detach the process from the active screen and push it into the background daemon matrix.

Managing the Output Stream

Because the process is now executing blindly in the background, it cannot print standard output (stdout) or standard error (stderr) to your terminal screen. The nohup engine algorithmically solves this by intercepting all output and violently redirecting it into a newly generated file named nohup.out within your current working directory. You can monitor the progress of your shielded script at any time by executing tail -f nohup.out.

Get the best tech tips delivered straight to your inbox.

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