Quick take: The nohup command runs a command immune to hangups, so it keeps running after you log out. Combine it with & to background the job: nohup ./long-task.sh &. Output goes to nohup.out unless redirected.
Introduction
When you log out or your SSH session drops, the shell normally sends a hangup signal that kills its child processes. The nohup command (no hangup) shields a command from that signal so it keeps running — essential for long jobs you want to survive a disconnect.
Syntax
The basic syntax of the nohup command is:
nohup COMMAND [ARGUMENTS] &Common Options and Parameters
The most useful options and parameters for the nohup command:
| Option | Description |
|---|---|
| nohup CMD | Run CMD ignoring the hangup signal. |
| nohup CMD & | Run it in the background as well. |
| > file | Redirect output to a chosen file. |
| 2>&1 | Also capture errors into the same file. |
| nohup.out | Default output file when none is given. |
Practical Examples
Real nohup commands you can run today:
# Run a job that survives logout
nohup ./backup.sh &
# Redirect output to a chosen log
nohup ./backup.sh > backup.log 2>&1 &
# Run a long Python script in the background
nohup python train.py > train.log 2>&1 &
# Check the default output file
tail -f nohup.out
# Find the backgrounded job's PID
jobs -lTips and Best Practices
- Always add
&to background the job, and redirect output (> file 2>&1) so you control where logs go instead of clutteringnohup.out. - For more control over detached, restartable sessions, use a terminal multiplexer like
tmuxorscreen. - Note the PID (
jobs -lorecho $!) so you can monitor or stop the job later.
Final Thoughts
nohup keeps long-running commands alive after you disconnect by ignoring the hangup signal, and paired with & it runs them in the background. Redirect the output so logs land where you want, and note the PID for later. For interactive, reattachable long sessions, tmux or screen go a step further.
FAQ: nohup Command in Linux
How do I keep a process running after logout?+
Start it with nohup and background it: nohup ./script.sh &. The nohup shields it from the hangup signal sent at logout, so it keeps running.
Where does nohup send output?+
By default to a file called nohup.out in the current directory. Redirect it explicitly with > file 2>&1 to choose the location and capture errors too.
What is the difference between nohup and &?+
& backgrounds a job in the current shell but it can still be killed at logout. nohup makes the command ignore the hangup signal so it survives logout. Use them together for a detached, persistent job.
How do I run a long script in the background?+
Use nohup script > script.log 2>&1 &. This runs it detached from the terminal, logs output to script.log, and returns your prompt immediately.
Should I use nohup or tmux?+
nohup is simplest for fire-and-forget jobs. tmux or screen are better when you want to reattach to an interactive session later, since they keep a full terminal alive that you can return to.
Need help with Linux servers or infrastructure?
Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.
Hire Me for Support