Skip to content
Go back

Differences Between nohup, disown, and & in Linux

· Updated:
By SumGuy 5 min read
Differences Between nohup, disown, and & in Linux

In the world of Linux, managing processes efficiently is crucial for system administrators. Three common tools that help in managing long-running processes are nohup, disown, and the & operator. Each serves a unique purpose but they are often used in conjunction to manage background processes and ensure they continue running even after a user has logged out. Let’s delve into the details of each, explore their differences and similarities, and share some tips and tricks for using them effectively.

What is nohup?

nohup, short for “no hang up,” is a command that is used to run another command that ignores the HUP (hangup) signal. This signal is sent to a process when its controlling terminal is closed (for example, when the user logs out). By ignoring this signal, the process continues running in the background even after the user logs out.

Syntax:

nohup command-to-run &

Example:

nohup python script.py &

Tips and Tricks:

nohup python script.py > output.log &

What is disown?

disown is a shell builtin command that removes a shell job from the shell’s job table, making it no longer a child of the shell. This means the process will not receive a HUP signal from the shell when the shell session is terminated.

Usage:

python script.py &
jobs
disown %1

Tips and Tricks:

What is &?

The & operator in Linux is used to put a command in the background. It’s a way to start a process and return immediately to the shell prompt, without waiting for the process to complete.

Example:

python script.py &

Tips and Tricks:

fg %1

Differences and Similarities

Understanding the nuances of nohup, disown, and & can greatly enhance your ability to manage processes on a Linux system. Each tool has its specific use case, and knowing when and how to use them can make your system administration tasks more efficient. Whether you are running scripts that take a long time to complete or setting up services that should run continuously, these tools are indispensable in a Linux environment.

The Gotchas Nobody Warns You About

Knowing what these tools do is great. Knowing where they’ll quietly betray you is better.

nohup.out will eat your disk if you forget about it

Run a chatty process with nohup and walk away. Come back a week later and find a nohup.out file the size of a small country sitting in whatever directory you launched it from. The fix is obvious in hindsight:

Terminal window
# Redirect stdout and stderr both — nohup.out won't be created at all
nohup python long_running_script.py > /var/log/myscript.log 2>&1 &
# Or send everything to /dev/null if you genuinely don't care
nohup python long_running_script.py > /dev/null 2>&1 &

The 2>&1 part redirects stderr to wherever stdout is going. Skipping it means errors still land in nohup.out while your script thinks it’s writing to the log. Classic.

disown doesn’t work in non-interactive shells

If you’re calling disown inside a script, it’ll fail silently or throw an error depending on the shell. disown is a shell builtin that only exists in interactive bash/zsh sessions — there’s no job table in a non-interactive script context. If you need persistent background processes from a script, nohup is your tool.

& with exit in a subshell will still kill your process

You backgrounded the job, you logged out, and it died anyway. Here’s a situation that catches people:

Terminal window
# This looks right but the subshell exit can still nuke the job
bash -c "long_running_thing &"
# Do this instead:
bash -c "nohup long_running_thing > /dev/null 2>&1 &"

The child process inherits SIGHUP from the parent shell when that shell exits. Without nohup in the mix, & alone isn’t enough protection.

The “I already started the process” rescue pattern

You launched something interactively and realized five minutes in that it’s going to take two hours. You don’t want to kill it and start over. Here’s the move:

Terminal window
# Suspend the foreground process
Ctrl+Z
# Send it to background
bg %1
# Now detach it from the shell so logout won't kill it
disown %1
# Verify it's gone from the job table
jobs

No PID hunting, no restart. This is the pattern that makes disown actually worth knowing — it’s the ejector seat for processes you forgot to nohup at launch.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
CUDA vs ROCm vs CPU: Running AI on Whatever GPU You've Got
Next Post
Stop Putting Passwords in Docker ENV

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts