nohup Command in Linux: Run Commands After Logout

By 

Updated on

7 min read

Using the nohup command in Linux to run commands after logout

When you run a long task over SSH and the connection drops, the process is terminated along with the session. The nohup command prevents that. It runs a given command and ignores the SIGHUP (hangup) signal, so the process keeps running even after you log out or close the terminal.

This guide explains how to use nohup to run commands that survive session disconnects, redirect their output, and manage them after they start.

nohup Syntax

txt
nohup [OPTION]... COMMAND [ARG]...

nohup supports only a small set of options: -h, --help, -V, and --version. In normal use, everything after nohup is treated as the command to execute.

Running a Command with nohup

To run a command that will not be killed when the terminal closes, prefix it with nohup:

Terminal
nohup mycommand
output
nohup: ignoring input and appending output to 'nohup.out'

nohup runs mycommand in the foreground and redirects its output to a file called nohup.out in the current working directory . If you do not have write permission in that directory, the file is created in your home directory instead.

If you log out or close the terminal, the process continues running in the background.

Running in the Background

Running nohup in the foreground is not very useful because you cannot interact with the shell until the command finishes. To run the command in the background , append & at the end:

Terminal
nohup mycommand &
output
[1] 25177

The output shows the shell job ID in brackets and the process ID. You can continue using the terminal immediately while the command runs in the background.

To bring the command back into the foreground, use the fg command . To check which background jobs are active in the current shell, use jobs:

Terminal
jobs
output
[1]+  Running                 nohup mycommand &

Redirecting Output to a File

By default, nohup sends all output to nohup.out. To redirect to a different file, use standard shell redirection.

Redirect both standard output and standard error to a single file:

Terminal
nohup mycommand > mycommand.out 2>&1 &

Redirect standard output and standard error to separate files:

Terminal
nohup mycommand > mycommand.out 2> mycommand.err &

If you do not need the output at all, redirect it to /dev/null:

Terminal
nohup mycommand > /dev/null 2>&1 &

For more on redirection syntax, see the guide on redirecting stderr and stdout .

Reading nohup Output

When a nohup process writes to nohup.out over a long period, you can follow the output in real time with tail -f :

Terminal
tail -f nohup.out

This displays new lines as they are written to the file. Press Ctrl+C to stop following.

To see only the last 20 lines of the file:

Terminal
tail -20 nohup.out

Checking and Managing nohup Processes

After you start a nohup process and close the terminal, the shell job ID is no longer available. You can find the process using ps :

Terminal
ps aux | grep mycommand
output
john     25177  0.2  1.3 124568 53712 ?        S    14:03   0:05 mycommand

The ? in the TTY column confirms the process is no longer attached to a terminal.

To stop the process, send it a signal with kill :

Terminal
kill 25177

If the process does not respond to a regular SIGTERM, force it with SIGKILL:

Terminal
kill -9 25177

Using nohup with sudo

When you need to run a nohup process as root, place sudo before nohup:

Terminal
sudo nohup ./long-task.sh > /var/log/long-task.out 2>&1 &

If you place nohup before sudo instead, the sudo password prompt may not work correctly because nohup redirects standard input.

Practical Examples

Run a backup script that continues after you log out:

Terminal
nohup /usr/local/bin/backup.sh > /var/log/backup.out 2>&1 &

Download a large file with wget in the background:

Terminal
nohup wget https://example.com/large-file.tar.gz &

wget writes its own progress to nohup.out, so you can check tail -f nohup.out to monitor the download.

Run a Python script that processes data overnight:

Terminal
nohup python3 process_data.py > process.log 2>&1 &

Alternatives to nohup

nohup is the simplest way to keep a command running after logout, but other tools offer more control.

disown

disown is a shell builtin that detaches a running job from the current shell. Unlike nohup, you can use it on a process that is already running:

Terminal
mycommand &
disown %1

After disown, the shell will not send SIGHUP to that job when you log out. The difference from nohup is that disown does not redirect output, so if the process writes to the terminal and the terminal closes, the process may fail with a write error.

Screen

Screen is a terminal multiplexer that lets you create persistent sessions. You can detach from a session and reattach later, even from a different SSH connection. This gives you full interactive access to the process, not just background execution.

Tmux

Tmux is a modern terminal multiplexer and a popular alternative to Screen. It offers the same session persistence with a more flexible interface for splitting panes and managing multiple windows.

For simple background tasks where you do not need to interact with the process later, nohup is the most straightforward option. Use Screen or Tmux when you need to reattach and see live output.

Quick Reference

CommandDescription
nohup command &Run a command in the background, immune to hangup
nohup command > file.out 2>&1 &Redirect all output to a custom file
nohup command > /dev/null 2>&1 &Discard all output
tail -f nohup.outFollow nohup output in real time
ps aux | grep commandFind a running nohup process
kill PIDStop a nohup process gracefully
kill -9 PIDForce-stop a nohup process
jobsList background jobs in the current shell
disown %1Detach job 1 from the shell (no SIGHUP on logout)

Troubleshooting

nohup: failed to run command: Permission denied
The command you passed to nohup is not executable. Check the file permissions with ls -l and add the execute bit with chmod +x script.sh if needed.

nohup: ignoring input and appending output to ’nohup.out’
This is not an error. It is the normal message nohup prints to confirm that standard input is disconnected and output is going to nohup.out. The process started successfully.

The process stopped after logout even though I used nohup
This can happen if the shell sends SIGTERM instead of SIGHUP, or if the process depends on a terminal for input. Make sure you are also using & to put the process in the background, and redirect standard input if the command tries to read from it.

nohup.out is growing very large
Redirect output to /dev/null if you do not need it (nohup command > /dev/null 2>&1 &), or redirect to a specific log file and set up log rotation.

FAQ

What does nohup stand for?
nohup stands for “no hangup.” It prevents the SIGHUP signal from reaching the command, so the process is not terminated when the controlling terminal closes.

What is the difference between nohup and disown?
nohup must be used before starting the command. It redirects output and makes the process immune to SIGHUP from the start. disown is a shell builtin that detaches an already-running job from the shell, but it does not redirect output. If you forgot to use nohup, you can background the process with Ctrl+Z and bg, then run disown to detach it.

What is the difference between nohup and screen/tmux?
nohup simply prevents a process from being killed on logout. You cannot interact with the process after it starts. Screen and Tmux create persistent terminal sessions that you can detach from and reattach to later, giving you full interactive access.

Where does nohup save its output?
By default, nohup writes to nohup.out in the current working directory. If the directory is not writable, it writes to $HOME/nohup.out. You can override this with shell redirection.

Can I use nohup with a pipeline?
Yes, but you need to wrap the pipeline in a shell:

Terminal
nohup bash -c 'command1 | command2 > output.log 2>&1' &

Without the bash -c wrapper, nohup only applies to the first command in the pipeline.

Conclusion

nohup is the simplest way to keep a process running after you close a terminal or disconnect from SSH. For tasks where you need to reattach and interact with the process later, consider using screen or tmux instead.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page