Nohup is a POSIX command that runs another program immune to hangup signals, so the process keeps running even after you log out or close the terminal. You use it by typing nohup command &, and it writes output to nohup.out by default. This article explains how nohup works, why it exists, and the practical ways to use it in daily Linux and Unix work.
Background: Why Nohup Exists
In the early days of Unix, terminals were physical devices connected by serial lines. When a user logged out, the system sent a SIGHUP (hangup) signal to every process started from that terminal, telling them the line was going away. Programs that ignored SIGHUP could keep running but would write to a dead terminal, which caused errors. Nohup was created as a simple wrapper: it starts your command with SIGHUP ignored, then execs the command, so the signal has no effect. The name is short for “no hangup.”
Modern terminal emulators and SSH sessions still send SIGHUP when you close a window or disconnect, so nohup remains useful on Linux, macOS, and other Unix-like systems. It is not a job scheduler and does not daemonize the process; it only protects against one specific signal.
How Nohup Works
When you run nohup command, the following happens:
- Nohup sets the SIGHUP signal disposition to ignore.
- If standard output is a terminal, it redirects output to a file named
nohup.outin the current directory (or~/nohup.outif the current directory is not writable). - If standard error is a terminal, it redirects stderr to the same file as stdout.
- It then runs your command with those settings.
The & at the end puts the command in the background, so you get your shell prompt back immediately. Without &, the terminal will stay occupied until the command finishes, which defeats the purpose of logging out.
You can also redirect output yourself: nohup command > my.log 2>&1 &. This sends both stdout and stderr to my.log, and nohup will not create nohup.out.
Basic Usage Examples
Here are the most common patterns:
nohup ./long_task &– runs long_task in the background, logs to nohup.out.nohup python3 script.py > script.log 2>&1 &– logs to a specific file.nohup ./server --port 8080 < /dev/null &– detaches stdin so the process does not read from the terminal.nohup bash -c 'cmd1 && cmd2' &– runs a compound command.
After starting, you can check the process with ps aux | grep long_task or pgrep -f long_task. To stop it, use kill PID (find the PID from ps).
Nohup vs. disown vs. screen vs. tmux
Nohup is often compared to other ways of keeping processes alive:
- disown – a shell built-in that removes a job from the shell’s job table, so it won’t receive SIGHUP when the shell exits. Unlike nohup, disown does not redirect output and only works on jobs already started in that shell.
- screen – a terminal multiplexer that creates a virtual terminal. You can detach from it (
Ctrl-a d) and reattach later (screen -r). This lets you interact with the process, not just leave it running. - tmux – a modern alternative to screen with similar detach/attach features and better scripting.
Use nohup when you only need the process to survive logout and don’t need to interact with it. Use screen or tmux when you need to come back and type commands or see output in a live terminal.
Common Problems and Pitfalls
Several issues trip up new users:
- Output not appearing in nohup.out – if you redirected stdout yourself, nohup.out won’t be created. Also, if the program buffers output (like Python with
print), you may not see it until the buffer fills or the process exits. Usepython3 -uto force unbuffered output. - Process dies anyway – nohup only ignores SIGHUP. If the process receives SIGTERM, SIGINT, or crashes, it will still stop. Also, if you run nohup inside a script that exits, the process may get other signals.
- nohup.out grows huge – for long-running tasks, redirect to a log file and use log rotation.
- Can’t find the process later – use
pgrep -forps aux | grepto locate it. The PID is printed when you start nohup, so write it down if you need it. - Permission denied on nohup.out – if the current directory is not writable, nohup tries to write to
~/nohup.out. If that also fails, the command won’t start.
How to Choose the Right Tool
Decide based on what you need:
- Just keep a script running after logout? → nohup.
- Need to interact with the process later? → screen or tmux.
- Already started a job and forgot nohup? → disown (if the shell hasn’t exited).
- Need the process to survive reboots? → use a service manager like systemd (create a unit file) or cron with
@reboot.
For production services, systemd is more robust because it can restart the process, manage dependencies, and log properly. Nohup is best for ad-hoc tasks and quick experiments.
Nohup and Background Jobs: A Misconception
Many people think nohup is required to run a background job. That’s not true. The & alone puts a job in the background, but it will still receive SIGHUP when the shell exits. Nohup protects against that signal. Conversely, you can use nohup without & – the process will run in the foreground but ignore SIGHUP. For the typical use case (run in background and survive logout), you need both.
Another misconception: nohup makes the process a daemon. It doesn’t. A daemon typically detaches from the terminal, changes its working directory to /, closes file descriptors, and runs as a child of init. Nohup only ignores SIGHUP. If you need a true daemon, use daemon() in C or a tool like daemonize.
Frequently Asked Questions
Does nohup work on Windows?
Windows does not have a native nohup command. However, if you use Windows Subsystem for Linux (WSL) or a Unix-like environment like Cygwin, you can use nohup. In PowerShell, you can achieve similar behavior with Start-Process -NoNewWindow or by using Start-Job, but the semantics differ.
How do I stop a nohup process?
Find the PID with ps aux | grep your_command or pgrep -f your_command, then run kill PID. If the process doesn’t stop, use kill -9 PID. Note that kill -9 cannot be caught, so use it as a last resort.
Where does nohup write output if I don’t redirect?
It writes to nohup.out in the current working directory. If that directory is not writable, it tries ~/nohup.out (your home directory). If both fail, the command will not run.
Can I use nohup with a command that has arguments?
Yes. Just put the entire command after nohup: nohup python3 script.py --input data.txt &. The shell parses the arguments normally, and nohup runs the whole thing.
Does nohup work with cron jobs?
Cron jobs already run without a terminal and do not receive SIGHUP, so nohup is unnecessary. If you need to run a long job from cron, just call the command directly; cron handles the environment.
What is the difference between nohup and setsid?
setsid starts a process in a new session, which detaches it from the controlling terminal entirely. This also prevents SIGHUP, but it goes further: the process has no controlling terminal at all. Nohup only ignores one signal. For most purposes, either works, but setsid is closer to a true daemon.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Reliable Subaru vs New Honda Civic Cost
- Pokemon Bookmark Corner Designs & Pokemon GO DIY
- Forgotten Games That Created Your Favorite Genres
- Lexus vs Genesis Luxurious Compact SUV0.075 hp/lb
- Uber VW Robotaxi LA Launch Date
- Notebook vs Tablet: Which Saves You More Money?