← Back to list

Mastering nohup: Keep Your Linux Jobs Alive After Logout or SSH Disconnect

When you close an SSH session or accidentally lose your terminal connection, any processes running in that session typically receive a…

Hecate · 2025-08-28 17:10 · 0 claps · 2.5 min read paywalled
#nohup #linux #ssh #code #tips-and-tricks
Open on Medium ↗
Wiki topics: 🔓 · Open Source 🏃 · Running & Endurance

Mastering nohup: Keep Your Linux Jobs Alive After Logout or SSH Disconnect

When you close an SSH session or accidentally lose your terminal connection, any processes running in that session typically receive a SIGHUP (Signal Hang Up) signal, causing them to terminate immediately. This can be frustrating if you’re running long tasks or deployments that you expect to continue even after you disconnect.

The **nohup (no hang up)** command solves this problem by blocking the SIGHUP signal and allowing your processes to continue running safely in the background—even after you log out.

This guide walks through everything you need to know about nohup on Linux, including its syntax, practical examples, process management tips, and best practices.

Why Use nohup for Background Processes?

nohup provides several advantages for long-running or critical jobs:

  • Prevents premature termination — processes won’t stop if you log out or disconnect.
  • Avoids wasted compute time — no need to restart tasks from scratch if your session ends.
  • Ideal for remote work — resilient against unexpected SSH drops.
  • Detaches jobs cleanly — allows you to disown processes from your terminal session.
  • Ensures persistence — lets jobs run to completion even after you log off.

In short, nohup lets you start tasks with confidence, knowing they’ll finish regardless of terminal state.

Nohup Command Syntax

The basic syntax is:

nohup COMMAND [OPTIONS]

Common options:

  • --help – display usage information
  • --version – show the installed nohup version
  • -v, --verbose – enable detailed output (useful for debugging)
  • -p, --prefix PREFIX – specify a prefix path for the nohup.out file

How to Use nohup

  1. Run a basic command with nohup
nohup bash example.sh
  1. Run in the background

Append an ampersand (&) to detach the process from your terminal:

nohup bash example.sh &
  1. Redirect output to a custom file

By default, nohup writes output to nohup.out. You can redirect it elsewhere:

nohup bash example.sh > output.txt
  1. Run multiple commands sequentially

Use bash -c with a string of commands separated by &&:

nohup bash -c 'command1 && command2'

How to Terminate a nohup Process

  1. Find the process ID (PID) using ps and grep:
ps aux | grep "<pattern>"
  • a – show processes for all users
  • u – show detailed process info (user, CPU%, memory%, etc.)
  • x – include processes without a controlling terminal
  • | – pipe output into another command
  • grep – filter for a specific keyword (e.g., apache, 8007)
  1. Kill the process using its PID:
kill <PID>

Practical nohup Use Cases

Example: Keep a Django Development Server Running

# Start the server and detach it
nohup python3 manage.py runserver 0.0.0.0:8007 &

# Find the running process
ps aux | grep "8007"

# Kill the process when done
kill 943507

Key Takeaways

  • **nohup prevents your jobs from dying when your SSH session closes.**
  • Always redirect output if you want cleaner logs.
  • Combine with & to run jobs fully in the background.
  • Use ps aux | grep to locate processes and kill to terminate them.

With these techniques, you can safely log out or disconnect from your SSH session while ensuring your tasks keep running to completion.


메타데이터
post_id
50b9bca83aeb
slug
mastering-nohup-keep-your-linux-jobs-alive-after-logout-or-ssh-disconnect-50b9bca83aeb
url
https://medium.com/@hecate_he/mastering-nohup-keep-your-linux-jobs-alive-after-logout-or-ssh-disconnect-50b9bca83aeb
canonical_url
https://medium.com/@hecate_he/mastering-nohup-keep-your-linux-jobs-alive-after-logout-or-ssh-disconnect-50b9bca83aeb
author_url
https://medium.com/@hecate_he
status
ok
fetched_at
2026-06-26 21:52:29