How to Use the nohup Command in Linux

How to Use the nohup Command in Linux

A long task started in a Linux terminal can stop when you close that terminal, because the shell can send it a hangup signal. The nohup command starts a program with that signal ignored, so it keeps running.

I’ll cover running a job with nohup and & so you get your prompt back, and where its output ends up.

What is the nohup command?

The nohup command starts a utility with the hangup signal (SIGHUP) set to ignored. A shell can send that signal when a terminal session closes.

The shell’s ampersand operator backgrounds a command and returns the prompt. Pair it with nohup when the job also needs to ignore SIGHUP.

  • nohup changes how the started utility handles SIGHUP.
  • & asks the shell to run the command asynchronously.
  • The pair does not monitor or restart the process after a failure.
  • A system reboot ends a nohup job instead of restarting it.

The nohup manual page describes its signal and output behavior, and LinuxForDevices has a guide to finding other manual pages.

Prerequisites for these examples

The examples use GNU nohup and Bash-style job control, and the log directory must be writable. Check that the command is on your PATH before starting.

Check that nohup is on your PATH before starting:

command -v nohup

If the command runs a script, confirm that the script path and its interpreter are available too. The Linux shell-script guide covers how to launch a .sh file from a terminal.

Step 1: Start a command that can outlast the terminal

Pair nohup’s signal handling with the shell operator that returns the prompt.

Run nohup with the ampersand when you want the prompt back

Run nohup without an ampersand and the command stays in the foreground, as the ping screenshot below shows.

nohup ping running in the foreground and reporting nohup.out
Without an ampersand, nohup does not return the shell prompt while ping runs

Add an ampersand after the command, and the shell returns a job number and process ID while ping runs.

nohup ping started as a background shell job
The ampersand returns the shell prompt while the ping job runs

This short test writes both output streams to job.log. The wait command shows the three-second test’s finished output in one terminal capture.

I ran the short shell job shown here, and its log contained started, progress on stderr, and finished. The stderr line landed in the same file because of 2>&1.

Leave wait and cat out for a long-running job so the prompt returns immediately.

nohup sh -c 'printf "started\n"; printf "progress on stderr\n" >&2; sleep 3; printf "finished\n"' > job.log 2>&1 < /dev/null & pid=$!; printf 'PID: %s\n' "$pid"; wait "$pid"; cat job.log
nohup captures standard output and standard error in job.log
A background shell job writes both output streams to job.log

The shell expands $! to the process ID of the last background job. Here the process writes one message to standard error as well as standard output, and 2>&1 sends both into job.log.

For a script you already have, replace the short sh command with its executable path and arguments. The shell-script execution steps help when the file is not executable yet.

Step 2: Choose where nohup writes output

If standard output is still connected to a terminal, GNU nohup appends it to nohup.out in the current directory, or to $HOME/nohup.out if the first path cannot be opened. If standard error is also connected to the terminal, GNU nohup redirects it to standard output.

Explicit redirection makes the destination predictable. The order of 2>&1 matters because it sends standard error to the destination that standard output already has.

Shell syntaxEffect
> task.logWrite standard output to task.log and replace its previous contents.
>> task.logAppend standard output to task.log instead of replacing it.
Redirect stderrSend standard error to the current standard-output destination.
< /dev/nullGive the command no interactive input from the terminal.

The nohup manual describes the default file behavior, while POSIX documents the current-directory file and HOME fallback. To inspect a log after the job ends, use the cat and more commands.

Step 3: Check and stop the process

Identify the intended PID first, then send a signal and check how the process ended.

Find the process ID before sending a signal

Keep the PID printed by the shell or saved in a variable. The ps command can check whether that process exists at the moment you run it.

The process screenshot shows ps aux | grep ping. That is a quick search, but grep can match itself or another command whose text contains ping, so check the PID column before acting. LinuxForDevices has a ps command reference for its other output fields.

ps output listing ping, grep, and a process with no controlling terminal
The question mark is the TTY field. This process listing does not prove nohup was used

Once you have the intended PID, send a termination signal and then collect the result.

Send SIGTERM and check the wait status

Use kill with the PID to ask the process to stop. With no signal option, kill sends SIGTERM, which nohup does not ignore.

nohup sleep 20 > stop-demo.log 2>&1 < /dev/null & pid=$!; printf 'PID: %s\n' "$pid"; ps -p "$pid" -o pid=,stat=,args=; kill "$pid"; if wait "$pid"; then printf 'exit=0\n'; else status=$?; printf 'exit=%s\n' "$status"; fi
kill sends a termination request to a nohup process PID
kill requests termination by PID, but the command alone does not confirm the process has exited
ps shows a nohup sleep process before kill returns status 143
The process is listed before SIGTERM, and wait reports its signal exit status

I used kill on a test sleep process, and Bash wait returned status 143. That result records termination by signal 15 rather than normal completion.

A kill command only sends a signal. If the process handles SIGTERM and keeps running, inspect it again before sending a stronger signal. The Linux kill command guide explains signal choices.

Know where nohup stops being enough

nohup ignores SIGHUP for the command it starts, but it does not supervise the process or keep it alive through a reboot, an application crash, a resource limit, or a different signal.

A graphical application depends on its desktop session, and the VLC screenshot shows that nohup plus an ampersand starts a shell job but does not prove a window stays usable after logout.

VLC launched with nohup and the shell ampersand
nohup vlc starts a shell job, but the screenshot does not prove the desktop session will persist

The Django runserver screenshot shows a background command rather than a production deployment recipe. Django documents its lightweight development server as unsuitable for production and recommends a WSGI or ASGI server for production.

Django development server command started with nohup and output redirected to server_logs
The shell reports a background job, and the command redirects its output to server_logs

If a command must restart after failure, start at boot, or have a managed lifecycle, use a service manager rather than nohup. LinuxForDevices explains how to start, stop and restart Linux services and how to list services in Ubuntu.

Use a service manager when lifecycle matters

For a one-time non-interactive task, nohup with explicit logs is a small tool for a narrow job. For a service that must restart or start during boot, configure a service manager and its policy instead.

systemd service units can define restart behavior, while nohup only changes how the launched command handles hangups. Read the systemd service documentation and LinuxForDevices’ guide to restarting a Linux system before you treat a detached process as a managed service.

man nohup

nohup command questions

Does nohup run a command in the background?

No. nohup makes the started command ignore SIGHUP. Add the shell ampersand operator when you want the command to run in the background.

Where does nohup write command output?

When standard output is a terminal, GNU nohup appends output to nohup.out in the current directory, or to $HOME/nohup.out if that file cannot be opened. Redirect output to a named file to choose another destination.

How do I stop a nohup process?

Find its PID, send a signal with kill, then check the process or collect its exit status. nohup ignores SIGHUP, not SIGTERM.

Will nohup keep a process running after a reboot?

No. nohup does not supervise or restart a process after a reboot. Use a service manager when the command needs a managed lifecycle.