Skip to content
Go back

Differences Between nohup, disown, and & in Linux

· Updated:
By KingPin 6 min read
Differences Between nohup, disown, and & in Linux

In the world of Linux, managing processes efficiently matters 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 dig 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. The process is still a child of the shell, but the shell no longer knows about it, so it will not send the job a HUP signal 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. These tools are indispensable in a Linux environment, whether you are running scripts that take a long time to complete or setting up services that should run continuously.

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, so 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 is mostly pointless in scripts

disown is a bash/zsh builtin, and it does run inside a script. It rarely matters there, because a non-interactive shell does not send SIGHUP to its jobs on exit anyway. The real threat is the terminal hanging up on a process that was started from it. If you need a background process to ignore that signal, nohup is your tool.

& alone does not ignore SIGHUP

You backgrounded the job, you logged out, and it died anyway. A job started with & from an interactive shell can still receive SIGHUP when the terminal closes. Starting it from a throwaway bash -c does not change that when you launch it from a terminal you then close:

Terminal window
# Still tied to your terminal's hangup
bash -c "long_running_thing &"
# Do this instead:
bash -c "nohup long_running_thing > /dev/null 2>&1 &"

Without nohup (or disown, or setsid) 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
Finding the PID of a Process Using a Specific Port in Linux
Next Post
the lost+found Directory in Linux

Discussion

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

Related Posts